如何在猫鼬中填充一个组
How to populate a group in mongoose
我的 mongoose 模式中有这个...和一些小组...
'use strict';
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var clientSchema = new mongoose.Schema({
name : { type: String },
offerings : [{ type: String }],
cscPersonnel : {
salesExec : { type: Schema.Types.ObjectId, ref: 'User' },
accountGM : { type: Schema.Types.ObjectId, ref: 'User' },
},
},
netPromoterScore : { type: Number }
});
module.exports = mongoose.model('clients', clientSchema);
我尝试以 dis 方式填充 reff...我也在 ref 中填充(用户作为 {path:'cscPersonnel'})
function getOneById(id){
var deferred = Q.defer();
console.log("im in get by id" +id);
model
.findOne({ _id: id })
.populate({path:'cscPersonnel'})//one way
/* 'cscPersonnel salesExec', //second way
'cscPersonnel accountGM', */
.exec(function (err, item) {
if(err) {
console.log(err);
deferred.reject(err);
}
else
console.log(item);
deferred.resolve(item);
});
return deferred.promise;
} // gentOneById method ends
但不幸的是出现了这个错误!!!!
CastError:路径“_id”处的值“[object Object]”转换为 ObjectId 失败
{
"message": "Cast to ObjectId failed for value \"[object Object]\" at path \"_id\"",
"name": "CastError",
"type": "ObjectId",
"value": {
"salesExec": "56cf5f09245f8a240b30693b",
"accountGM": "56cf5f09245f8a240b30693b"
},
"path": "_id"
}
如何让它解决这个问题.... 求助,在此先感谢
请试试这个
model
.findOne({ _id: id })
.populate({path: 'cscPersonnel.salesExec'})
.populate({path: 'cscPersonnel.accountGM'})
.exec(function (err, item) {
我的 mongoose 模式中有这个...和一些小组...
'use strict';
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
var clientSchema = new mongoose.Schema({
name : { type: String },
offerings : [{ type: String }],
cscPersonnel : {
salesExec : { type: Schema.Types.ObjectId, ref: 'User' },
accountGM : { type: Schema.Types.ObjectId, ref: 'User' },
},
},
netPromoterScore : { type: Number }
});
module.exports = mongoose.model('clients', clientSchema);
我尝试以 dis 方式填充 reff...我也在 ref 中填充(用户作为 {path:'cscPersonnel'})
function getOneById(id){
var deferred = Q.defer();
console.log("im in get by id" +id);
model
.findOne({ _id: id })
.populate({path:'cscPersonnel'})//one way
/* 'cscPersonnel salesExec', //second way
'cscPersonnel accountGM', */
.exec(function (err, item) {
if(err) {
console.log(err);
deferred.reject(err);
}
else
console.log(item);
deferred.resolve(item);
});
return deferred.promise;
} // gentOneById method ends
但不幸的是出现了这个错误!!!!
CastError:路径“_id”处的值“[object Object]”转换为 ObjectId 失败
{
"message": "Cast to ObjectId failed for value \"[object Object]\" at path \"_id\"",
"name": "CastError",
"type": "ObjectId",
"value": {
"salesExec": "56cf5f09245f8a240b30693b",
"accountGM": "56cf5f09245f8a240b30693b"
},
"path": "_id"
}
如何让它解决这个问题.... 求助,在此先感谢
请试试这个
model
.findOne({ _id: id })
.populate({path: 'cscPersonnel.salesExec'})
.populate({path: 'cscPersonnel.accountGM'})
.exec(function (err, item) {