猫鼬.find()起作用并返回错误? ....功能相同

时间:2019-03-19 16:42:55

标签: database mongodb express mongoose find

当我传递不同的参数时,相同的代码块可以工作,然后却无法工作,这让我有些困惑。

有问题的代码:

exports.profileGet = (req, res) => {
   // console.log(req.user._id)
const id = req.user._id.toString()
console.log(id)

User.find({ username: 'p' }).exec((err, user) => {
  if (err) {
    res.render('profile')
  }
  console.log(user)
})

Photo.find({ userId: id }).exec((err, results) => {
  if (err) {
    res.render('profile')
  }
  console.log(results)
})

}

console.log(id)在console.log中返回此值:

5c89a00e102515541329db1a

哪个是正确的用户ID。

然后User.find在控制台日志中返回此值:

[ { isAdmin: false,
_id: 5c89a00e102515541329db1a,
email: 'p',
username: 'p',
password:
 '$2b$10$CTihm9TXcbyYFEHZU8GruOT61kyBDTj7wFGg4QaQN6bIMpnKv6oiO',
__v: 0 } ]

这是正确的用户,显示.find()方法有效。

但是照片 .find在console.log中返回此值(即使我给了它适当的参数):

throw er; // Unhandled 'error' event
  ^

RangeError: Invalid count value
at String.repeat (<anonymous>)
at reduceToSingleString 
(internal/util/inspect.js:1216:27)
at formatRaw (internal/util/inspect.js:777:15)
at formatValue (internal/util/inspect.js:539:10)
at formatProperty (internal/util/inspect.js:1133:11)
at formatRaw (internal/util/inspect.js:760:9)
at formatValue (internal/util/inspect.js:539:10)
at formatValue (internal/util/inspect.js:527:18)
at formatProperty (internal/util/inspect.js:1133:11)
at formatArray (internal/util/inspect.js:958:17)
at formatRaw (internal/util/inspect.js:757:14)
at formatValue (internal/util/inspect.js:539:10)
at inspect (internal/util/inspect.js:196:10)
at Object.formatWithOptions (util.js:84:12)
at Console.(anonymous function) 
(internal/console/constructor.js:274:15)
at Console.log (internal/console/constructor.js:283:59)
Emitted 'error' event at:
at /Users/samuelchasan/Documents/ITP/thesis_app/node_modules/mongoose/lib/model.js:4700:13
at /Users/samuelchasan/Documents/ITP/thesis_app/node_modules/mongoose/lib/utils.js:263:16
at _hooks.execPost (/Users/samuelchasan/Documents/ITP/thesis_app/node_modules/mongoose/lib/query.js:4074:11)
at /Users/samuelchasan/Documents/ITP/thesis_app/node_modules/kareem/index.js:135:16
at processTicksAndRejections (internal/process/next_tick.js:74:9)

“照片”模型的参数与查询匹配:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const photoSchema = new Schema({
    name: { type: String },
    data: { type: Buffer },
    contentType: { type: String },
    userId: { type: String }
 });

module.exports = mongoose.model('photos', photoSchema);

如您在图像中所见,数据在mongo中,因此它应该找到文件...

Photo data in mongo database

我完全不知所措!

非常感谢您的帮助。

谢谢:)

0 个答案:

没有答案