猫鼬查询返回500错误

时间:2018-07-21 04:30:22

标签: javascript node.js mongodb express mongoose

问题

所以我的问题是我有这个猫鼬查询:

Tag.find().and([{ name: { $regex: input, $options: 'i' } }, { followers: {users: { $ne: ObjectId(user.id) }} }]).exec(callback)

接受用户输入,并找到一个与输入相似的名称字段,并且在其followers.users数组中没有user.id

但是无论何时我测试此代码,我的控制台都会出现500错误。

VM1418:1 GET http://localhost:3000/users/tag?input=s 500 (Internal Server Error)

代码

var TagSchema = new Schema({
  name: { type: String },
  followers: { users: [Schema.Types.ObjectId], total: Number },
  questions: { posts: [Schema.Types.ObjectId], total: Number }
}, { collection: 'tags' })

const Tag = mongoose.model('Tag', TagSchema)

module.exports.findTagsByUser = function (input, user, callback) {

    Tag.find().and([{ name: { $regex: input, $options: 'i' } }, { followers: {users: { $ne: ObjectId(user.id) }} }]).exec(callback)

}

// GET tags
router.get('/tag', function (req, res) {
  const input = req.query.input

  if (input) {
    Tag.findTagsByUser(input, req.user, function (err, tags) {
      if (err) throw err
      res.send(tags)
    })
  }
})

function getTags(input) {
const value = input.value

  // Request to GET tags
  $.get(`/users/tag?input=${value}`, function (data, status) {
    console.log('Status: ', status)
    console.log(data)
  })
}

0 个答案:

没有答案
相关问题