Node.js 使用Mongoose返回null填充
我对猫鼬和参考文件有问题。我在mongoose文档中看到了populate方法,但它工作得不好。这是我的代码: producto.js:Node.js 使用Mongoose返回null填充,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我对猫鼬和参考文件有问题。我在mongoose文档中看到了populate方法,但它工作得不好。这是我的代码: producto.js: var mongoose = require('mongoose'); var Schema = mongoose.Schema; module.exports = mongoose.model('Producto', { referencia: { type: String, unique: true,
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
module.exports = mongoose.model('Producto', {
referencia: {
type: String,
unique: true,
required: true
},
talla: Number,
color:{
type: Schema.ObjectId,
ref: 'Color'
},
foto: String,
precioCosto: Number,
precioVenta: Number,
descripcion: String,
tipoProducto:{
type: Schema.ObjectId,
ref: 'TipoProducto'
},
proveedor:{
type: Schema.ObjectId,
ref: 'Proveedor'
}
});
color.js:
var mongoose = require('mongoose');
module.exports = mongoose.model('Color', {
nombreColor: String,
codigoColor: String
});
查询:
Producto.find().populate('color').exec(function(err, productos){
console.log(productos);
if (err){
res.send(err);
}
res.json(productos);
});
在结果中,颜色为空:
[{
_id: "53618fd658dda21008000003"
color: null
descripcion: "11"
foto: "11"
precioCosto: 11
precioVenta: 11
proveedor: "53618f9fb3471b25ef46b504"
referencia: "11"
talla: 11
tipoProducto: "53618f15b3471b25ef46b503"
}]
提前感谢这意味着没有找到与
\u id
匹配的颜色。请检查您的基础收藏。我猜,当您创建颜色
对象时,您将其分配给了产品并保存了该对象,但是我打赌您从未保存颜色
对象本身,因此它不在集合中。感谢您的回答,我检查了de\u id
并且是相同的。另一方面,我不太明白如何用你的答案来解决这个问题,尼尔。