【发布时间】:2017-11-04 10:28:38
【问题描述】:
我有一个非常奇怪的问题,我的 Promise 的 .then 不起作用。错误如下:
TypeError: 无法读取未定义的属性“then”
我之所以称这很奇怪,是因为我在其他地方都使用相同的结构,而且它工作得完美无缺。我还想补充一点,它应该做的实际行动——确实完成了!所以这纯粹是我的 Promises “编码”方式的问题 - 只是语法糖。
我的代码 sn-p 如下:
var updateAllUnreadNotifications = function(){
userModel.aggregate([
{$match:{"profileID": 123}},
{$unwind: "$notifications"},
{$match:{"notifications.read": false}},
{$group: {_id:"$_id", notifications: {$push:"$notifications"}}}
], function(err, notifications){
var notificationPromises = notifications.map(function(notification) {
return new Promise(function(resolve){
userModel.findOneAndUpdate(
{profileID: 123, "notifications.id": notification.id},
{"notifications.$.read": true},
{safe: true},
function(err, result) {
if (err){
return;
} else if (result){
resolve(notification);
}
}
);
});
});
return Promise.all(notificationPromises);
});
};
updateAllUnreadNotifications()
.then(function(notifications){
res.json({
message: 'All is well'
});
});
此外,我在 notificationPromises 上做了一个 console.log,它看起来确实像一个 Promise 链。当然,result 变量也存在,所以这不是 DB 命令的问题。
我搞砸了什么?我确信这将是一个非常明显的问题,但对于我的生活,我无法孤立它。
如果需要更多信息,请告诉我。
【问题讨论】:
-
你没有从
updateAllUnreadNotifications返回任何东西。添加return userModel.aggregate([ ... ]); -
嘿伙计,感谢您指出这一点。我以为我的“return new Promise”行会涵盖它,但想一想,那是针对 .map 循环的。你介意把我引向正确的方向吗?不用说,有点JS新手。
-
@ShayanKhan 编辑您的问题。我没有看到
userModel.aggregate([对应的结束]括号 -
您似乎有一些语法错误,这是您使用的确切代码吗?
-
我错过了 userModel.aggregate 的“函数”行。现在,它已经完成了。我很抱歉。
标签: javascript node.js promise