[英]How to solve “TypeError: callback.apply is not a function”?
我正在做一個大學項目,並且閱讀過有關我的問題的每篇文章,但是我還沒有找到解決方案。 也許你可以幫我。
代碼如下:
viewerObj.update({_id: currentIDViewerVar} , {minutesWatched: 5},{upsert:true} , function (err,result) { if (err) throw err; console.log("Viewer " + userNameVar + " gespeichert"); console.log("minsWatched" +minsWatched); });
我收到以下錯誤。 我看不到我在做什么錯。
events.js:160 throw er; // Unhandled 'error' event ^ TypeError: callback.apply is not a function at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\lib\\model.js:3388:16 at Query.callback (C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\lib\\query.js:2185:9) at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\node_modules\\kareem\\index.js:259:21 at C:\\Users\\picco\\Desktop\\TwitchWatcher_v19\\TwitchWatcher\\node_modules\\mongoose\\node_modules\\kareem\\index.js:127:16 at _combinedTickCallback (internal/process/next_tick.js:67:7) at process._tickCallback (internal/process/next_tick.js:98:9) Process finished with exit code 1
先感謝您!
您使用了太多參數。
更改此:
viewerObj.update({_id: currentIDViewerVar} , {minutesWatched: 5},{upsert:true} , function (err,result) {
if (err) throw err;
console.log("Viewer " + userNameVar + " gespeichert");
console.log("minsWatched" +minsWatched);
});
對此:
viewerObj.update({_id: currentIDViewerVar, minutesWatched: 5}, {upsert:true}, function (err,result) {
if (err) throw err;
console.log("Viewer " + userNameVar + " gespeichert");
console.log("minsWatched" +minsWatched);
});
參見文檔:
如果要更新貓鼬文檔,則不會傳入查詢作為第一個參數。
請參閱Document#update的文檔 。
因此,此更新方法需要3個參數,第三個參數是回調,並且您傳入一個對象( {upsert: true}
),其中更新方法需要回調。 這就是為什么您獲得callback.apply is not a function
。 僅僅因為{ upsert: true }
不是一個函數。
就我而言,我遇到的是貓鼬(5.1.2)聚合方法的問題。 相同的代碼在<4.9上工作,但是在升級到5時出錯。
我剛剛在查詢中添加了大括號
User.aggregate([{
$match: {
isDeleted: 0,
isActive: 1,
_id: Mongoose.Types.ObjectId(userId)
}
}, {
$project: {
claps: 1,
showIcon: { $cond: [{ $gt: [{ $size: "$userBadges" }, 0] }, 1, 0] },
}
}])
.exec(function (err, data) {});
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.