这一点.find不是函数,猫鼬模式设计



我目前正在编写一个函数,该函数将为指定社会委员会中的每个用户创建通知(顺便说一句,这是一个社交网站),每当我运行这个:

User.statics.newSocietyNotification = function(req, socID, typeID, url, next){
  var tmpUsers = []
  Society.findById(socID, function(err, doc){
    console.log(doc)
    doc.committee.forEach(function(comMember, i){
      tmpUsers.push(comMember._uid)
    })
    this.find({'_id' : { $in : tmpUsers } }, function(err, docs){
      if(err){
        console.log(err)
      }
      if(docs.length == 0 || docs == null){
        next(false)
      }
      next(docs)
      console.log(docs)
    })
  })
}

使用以下消息使服务器崩溃:

TypeError: this.find is not a function
    at /home/ubuntu/workspace/models/user.js:368:10
    at Query.<anonymous> (/home/ubuntu/workspace/node_modules/mongoose/lib/model.js:3324:16)
    at /home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:259:21
    at /home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:127:16
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

如果有人知道我做错了什么,我会非常感激的帮助:)

编辑:使用用户。查找而不是'this'

TypeError: User.find is not a function
    at /home/ubuntu/workspace/models/user.js:368:10
    at Query.<anonymous> (/home/ubuntu/workspace/node_modules/mongoose/lib/model.js:3324:16)
    at /home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:259:21
    at /home/ubuntu/workspace/node_modules/mongoose/node_modules/kareem/index.js:127:16
    at nextTickCallbackWith0Args (node.js:420:9)
    at process._tickCallback (node.js:349:13)

您的this在第368行是指findById方法的回调函数的上下文。这个回调函数(它是一个对象,在底层)没有一个叫做find的方法。

相反,使用Society.find(...User.find(...在数据库中查找其他内容。

相关内容

最新更新