无法使用 JavaScript 承诺读取未定义的属性“then" [英] Cannot read property 'then' of undefined with JavaScript promises
问题描述
我知道乍一看这可能看起来像重复,但我已经看到了所有答案,告诉我要返回,但这是行不通的.
I understand at first glance this may look like a duplicate but I have seen all the answers for that tell me to put a return in but that is not working.
这是我的功能:
function removePastUsersFromArray(){
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);
})
};
我在这个函数中得到错误:
and I get the error at this function:
function displayRemovedPastUsersFromArray(){
removePastUsersFromArray().then(function(promises){
基本上是说我的 removePastUsersFromArray
是未定义的.但它不是因为上面明确存在并返回承诺??
basically saying that my removePastUsersFromArray
is undefined. but it isn't as it clearly exists above and returns promises??
推荐答案
基本上是说我的
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?
它存在,是的,但它不返回任何东西.您拥有的 return
在 then
回调中,但函数本身没有 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);
})
};
这篇关于无法使用 JavaScript 承诺读取未定义的属性“then"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!