Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在mongoose架构中使用ref填充集合失败_Javascript_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

Javascript 在mongoose架构中使用ref填充集合失败

Javascript 在mongoose架构中使用ref填充集合失败,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我想将信用集合填充到用户集合中,这是我的信用模式 const CreditSchema = new Schema({ userId: Schema.Types.ObjectId, credit: { type: Number, default: 0 }, log: [String] }) 我的用户模式是这样的 const UserSchema = new Schema({ fullName: { type: String, trim: tru

我想将信用集合填充到用户集合中,这是我的信用模式

const CreditSchema = new Schema({
  userId: Schema.Types.ObjectId,
  credit: {
    type: Number,
    default: 0
  },
  log: [String]
})
我的用户模式是这样的

const UserSchema = new Schema({
  fullName: {
    type: String,
    trim: true,
    required: true
  },
  email: {
    type: String,
    unique: true,
    lowercase: true,
    trim: true,
    required: true
  },
  password: {
    type: String,
    required: true
  },
  credit: {type: Schema.Types.ObjectId, ref: 'Credit' } //I suspect something is not right here
})
为什么我在下面的用户对象中看不到信用

const user = await User.findOne({_id: req.query.id})
.populate('credit')
.exec()

console.log(user) // credit field is not present?

如何保存文档?mongoDB中是否保存了信用字段?@GeorgeBailey在那里,信用的用户id与用户id匹配。@FarhanTahir是的,我有。