且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无法使用 JavaScript 承诺读取未定义的属性“then"

更新时间:2023-02-22 16:58:56

基本上是说我的 removePastUsersFromArray 未定义

不,它说 removePastUsersFromArray() 调用返回了 undefined,因为这就是您试图调用 then 的原因.

No, it says that the removePastUsersFromArray() call returned undefined, as that's what you're trying to call then upon.

它显然存在于上面并返回承诺?

it clearly exists above and returns promises?

它存在,是的,但它不返回任何东西.您拥有的 returnthen 回调中,但函数本身没有 return 语句.返回链接产生的promise:

It exists, yes, but it doesn't return anything. The return you have is inside the then callback, but the function itself does not have a return statement. return the promise that results from the chaining:

function removePastUsersFromArray() {
  return pullAllUsersFromDB().then(function(users_array) {
//^^^^^^
    var cookie_value = document.cookie.split('=') [1];
    const promises = []
    for (var i = 0; i < _USERS.length; i++) {
      if (_USERS[i].useruid == cookie_value){
        var logged_in_user = _USERS[i].useruid;
        promises.push(
          onChildValue(rootRef, 'users/' + logged_in_user + '/disliked_users/').then(formatUsers)
        );
        promises.push(
          onChildValue(rootRef, 'users/' + logged_in_user + '/liked_users/').then(formatUsers)
        );
      }
    }
    return Promise.all(promises);
  })
};