【问题标题】:MongooseError: Callback must be a function, got [object Object] with findOne mongooseMongooseError: Callback must be a function, got [object Object] with findOne mongoose
【发布时间】:2021-12-30 05:38:29
【问题描述】:

我有这段代码,我用这些查询在数据库中找到一个监视器

        const monitorfind = await monitorschema.findOne({ Price: { $lte: Number(reqbody.price) } }, {Size: {$gte: Number(reqbody.size - 8)}}, {hz: {$gte: Number(reqbody.hz - 25)}}, {Resolution: {$gte: Number(reqbody.res)}}, {ResponseTime: {$lte: Number(reqbody.ms +4)}})

当我运行代码时,我得到了这个错误:

throw new MongooseError('Callback must be a function, got ' + callback);
          ^

MongooseError: Callback must be a function, got [object Object]
    at Function.Model.$handleCallbackError (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\mongoose\lib\model.js:4924:11)
    at Function.findOne (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\mongoose\lib\model.js:2248:19)
    at C:\Users\Sochum\Desktop\pickitly_web_main\routes\monitors.js:23:49
    at Layer.handle [as handle_request] (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Sochum\Desktop\pickitly_web_main\node_modules\express\lib\router\index.js:275:10)

我该如何解决这个问题?

【问题讨论】:

    标签: node.js mongodb mongoose


    【解决方案1】:

    您需要在findOne 之后链接exec() 函数。如果我们想使用async-await,就需要它。将查询更改为以下应该可以:

    const monitorfind = await monitorschema.findOne({
      Price: { $lte: Number(reqbody.price) },
      Size: {$gte: Number(reqbody.size - 8)},
      hz: {$gte: Number(reqbody.hz - 25)},
      Resolution: {$gte: Number(reqbody.res)},
      ResponseTime: {$lte: Number(reqbody.ms +4)}
    }).exec()
    

    参考:https://masteringjs.io/tutorials/mongoose/promise

    如果您使用的是旧版本的猫鼬 (first answer 中的建议将回调函数作为第二个参数传递给 findOne

    【讨论】:

      【解决方案2】:

      我认为你的reqbody 是错误的模式。正确的模式是req.body。但是你使用任何中间件function 来传递数据,使用代码,否则你可以使用req.body 作为你的代码。

      试试这个

      const conditions = {
          Price: { $lte: Number(reqbody.price) },
          Size: {$gte: Number(reqbody.size - 8)},
          hz: {$gte: Number(reqbody.hz - 25)},
          Resolution: {$gte: Number(reqbody.res)},
          ResponseTime: {$lte: Number(reqbody.ms +4)}
      }
      
      monitorschema.findOne(conditions, function (err, monitor) {
      
          console.log(monitor) //If matching any data it is show here 
      
      });
      

      【讨论】:

        猜你喜欢
        • 2021-04-29
        • 2021-05-13
        • 2021-06-22
        • 1970-01-01
        • 2021-12-07
        • 1970-01-01
        • 1970-01-01
        • 2022-07-27
        • 2016-06-17
        相关资源
        最近更新 更多