Mongoose 错误: Right-hand 'instanceof' not callable 错误
我在mongoose中设定用户的comment结构为:
[
[{1}, {2}, ...],
[{4}, {5}, ...],
...
]
假设我回复5这条评论,便找到该用户的comment(即上段结构),map第一遍,map第二遍找id是否对应:
let result = User.findOne({name:blog}).then((item) => {
let reply_to = reply_to_id ? reply_to_id[0] : ''
console.log('reply_to', reply_to)
// item.comment 结构:[ [{_id1},{_id2},{...}], [{},{},{}], ... ]
// 如果是回复评论 则查询
if (reply_to)
if (item.comment.length > 0)
// 遍历外层数组
item.comment.map((comments, comments_index) => {
// 遍历内层数组
if (comments.length > 0)
comments.map((comment, comment_index) => {
// 找到被回复的 comment
if (comment._id == reply_to) {
// 能输出相应结果,但下句报错
console.log(comment._id == reply_to, comments_index, comment)
// 报错:Unhandled rejection TypeError: Right-hand side of 'instanceof' is not callable
item.comment[comments_index].splice(
item.comment[comments_index].length-1,
0,
{
name: username,
date: new Date(),
message: message,
picture_url: [],
like_number: 0,
diskile_number: 0,
}
)
console.log(item.comment[comments_index])
}
})
})
......
报错如下,找遍了git、Cnode毫无头绪:
Unhandled rejection TypeError: Right-hand side of 'instanceof' is not callable
at Array._cast (C:\nodejs\myblog\node_modules\mongoose\lib\types\documentarray.js:103:15)
at Array.splice (C:\nodejs\myblog\node_modules\mongoose\lib\types\array.js:565:20)
at comments.map (C:\nodejs\myblog\models\UserActions.js:160:41)
at Array.map (native)
at item.comment.map (C:\nodejs\myblog\models\UserActions.js:156:19)
at Array.map (native)
at User.findOne.then (C:\nodejs\myblog\models\UserActions.js:153:22)
at tryCatcher (C:\nodejs\myblog\node_modules\bluebird\js\release\util.js:16:23)
at Promise._settlePromiseFromHandler (C:\nodejs\myblog\node_modules\bluebird\js\release\promise.js:512:31)
at Promise._settlePromise (C:\nodejs\myblog\node_modules\bluebird\js\release\promise.js:569:18)
at Promise._settlePromise0 (C:\nodejs\myblog\node_modules\bluebird\js\release\promise.js:614:10)
at Promise._settlePromises (C:\nodejs\myblog\node_modules\bluebird\js\release\promise.js:693:18)
at Async._drainQueue (C:\nodejs\myblog\node_modules\bluebird\js\release\async.js:133:16)
at Async._drainQueues (C:\nodejs\myblog\node_modules\bluebird\js\release\async.js:143:10)
at Immediate.Async.drainQueues (C:\nodejs\myblog\node_modules\bluebird\js\release\async.js:17:14)
at runCallback (timers.js:672:20)
at tryOnImmediate (timers.js:645:5)
at processImmediate [as _immediateCallback] (timers.js:617:5)
各位仁兄能否指点一下,小弟感激不尽,卡了一天了毫无进展
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 es6 的 promise 替换掉 mongoose 自带的promise:
mongoose.Promise = global.Promise;
并且在mongoose所有返回为query的方法使用promise时,加上
.exec()
,比如:User.findOne({name:blog}).then()
改为User.findOne({name:blog}).exec().then()
试试。item.comment[comments_index] 这个debug 输出是什么?
这个地方按理说 不应该涉及到promise,mongoose啊,我感觉按照语义是 右边不是一个不可返回。。。