问题描述
我正在尝试使用 mongoose populate 函数,但作为回应,我得到了空数组,我已经看到多篇关于此的帖子
var MerchantSchema = new mongoose.Schema({包:[{ $type: mongoose.Schema.Types.ObjectId, ref: 'Package'}]},{typeKey: '$type',时间戳:{ createdAt:'created_at',updatedAt:'updated_at'}});module.exports = mongoose.model('Merchant', MerchantSchema);
这是我对基本模型的架构定义.
var mongoose = require('mongoose');
var PackageSchema = new mongoose.Schema({商家 ID:{ 类型:mongoose.Schema.Types.ObjectId,参考:'商家'},名称:字符串,original_price:数量,折扣价格:数量});module.exports = mongoose.model('Package', PackageSchema);
这就是我所指的模型.Package 模型和 Merchant 模型中的数据保存得很好.
但是如果我使用 populate 函数查询,我将返回一个空字符串
商家.findById( req.params.id, '包').populate('包').exec(函数(错误,商家){如果(错误)下一个(错误);别的res.status(200).json(商家);});
输出:
{"_id": "579b3b2dc2e8d61c0ecd2731",包":[]}
谁能帮帮我
更新:
好吧,确实发生了一些奇怪的事情.如果我尝试使用 merchant_id
填充 Package 文档,它会正常工作,但反过来不行.
包.找().populate('merchant_id').exec(函数(错误,包){如果(错误)下一个(错误);别的res.status(200).json(packages);});
输出:
[{"_id": "579b3b51c2e8d61c0ecd2732","name": "头发 + 指甲",原始价格":600,"discounted_price": 400,merchant_id":{"_id": "579b3b2dc2e8d61c0ecd2731","updated_at": "2016-07-29T11:17:37.474Z","created_at": "2016-07-29T11:17:01.216Z","name": "VLCC","logo_image": "http://vlccwellness.com/India/wp-content/uploads/2015/09/logo1.png?","cover_image": "http://image3.mouthshut.com/images/imagesp/925053993s.jpg?","__v": 1,标签":[],包":[579b3b51c2e8d61c0ecd2732"],工作时间": {开场时间":1000,关闭时间":2100,"假日": "星期日"},信息": {"description": "Lorem Ipsum","性别": "男性",服务": [头发"],在 MerchantSchema 中使用 $type 的类型插入.
var MerchantSchema = new mongoose.Schema({包:[{ 类型:mongoose.Schema.Types.ObjectId,参考:'包'}]},{typeKey: '$type',时间戳:{ createdAt:'created_at',updatedAt:'updated_at'}});module.exports = mongoose.model('Merchant', MerchantSchema);
根据您的商家文档中的包裹验证 ObjectId 数组.
I am trying to use mongoose populate function but in response I am getting empty array, I have seen multiple posts regarding this
var MerchantSchema = new mongoose.Schema({
packages: [{ $type: mongoose.Schema.Types.ObjectId, ref: 'Package'}]
},
{
typeKey: '$type',
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at'}
});
module.exports = mongoose.model('Merchant', MerchantSchema);
This is my schema definition for the base model.
var mongoose = require('mongoose');
var PackageSchema = new mongoose.Schema({
merchant_id: { type: mongoose.Schema.Types.ObjectId, ref: 'Merchant' },
name: String,
original_price: Number,
discounted_price: Number
});
module.exports = mongoose.model('Package', PackageSchema);
And this is the model I am referring to. The data inside the Package model and Merchant model is being saved just fine.
But if I query using populate function I am being returned an empty string
Merchant
.findById( req.params.id, 'packages')
.populate('packages')
.exec(function (err, merchant) {
if (err)
next(err);
else
res.status(200).json(merchant);
});
Output:
{
"_id": "579b3b2dc2e8d61c0ecd2731",
"packages": []
}
Can anyone help me
Update:
Ok something really odd is happening. If I try to populate the Package document with merchant_id
it is working but not the other way around.
Package
.find()
.populate('merchant_id')
.exec(function (err, packages) {
if(err)
next(err);
else
res.status(200).json(packages);
});
Output:
[
{
"_id": "579b3b51c2e8d61c0ecd2732",
"name": "Hair + Nails",
"original_price": 600,
"discounted_price": 400,
"merchant_id": {
"_id": "579b3b2dc2e8d61c0ecd2731",
"updated_at": "2016-07-29T11:17:37.474Z",
"created_at": "2016-07-29T11:17:01.216Z",
"name": "VLCC",
"logo_image": "http://vlccwellness.com/India/wp-content/uploads/2015/09/logo1.png?",
"cover_image": "http://image3.mouthshut.com/images/imagesp/925053993s.jpg?",
"__v": 1,
"tags": [],
"packages": [
"579b3b51c2e8d61c0ecd2732"
],
"work_hours": {
"opening_time": 1000,
"closing_time": 2100,
"holiday": "Sunday"
},
"information": {
"description": "Lorem Ipsum",
"gender": "Men",
"services": [
"Hair"
],
Use type insted of $type in MerchantSchema.
var MerchantSchema = new mongoose.Schema({
packages: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Package'}]
},
{
typeKey: '$type',
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at'}
});
module.exports = mongoose.model('Merchant', MerchantSchema);
Verify there is an array of ObjectId against packages in your Merchant document.
这篇关于猫鼬填充返回空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!