我正在使用2个架构的连接,如下所示: -
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var CompanySchema = new Schema({
name: String,
address: String,
address2: String,
url: String,
geoLat: String,
geoLon: String,
telephone: String,
npi:String,
active: Boolean,
tax_id:String,
businessTypeId:[
{type: Schema.Types.ObjectId, ref: 'BusinessType'}
],
partners:[{type :Schema.Types.Mixed, ref:'TradingPartners'}]
});
module.exports = mongoose.model('Company', CompanySchema);
架构2: -
'use strict';
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var TradingPartnersSchema = new Schema({
name: String,
id: String,
enrollment_required: Schema.Types.ObjectId,
supported_transactions: Schema.Types.Mixed,
is_enabled: Boolean,
clearinghouse:String,
last_updated:String
});
module.exports = mongoose.model('TradingPartners', TradingPartnersSchema);
现在我在我的控制器中使用下面的代码进行连接,这会引发错误: -
exports.findCompanyById = function(req, res) {
Company.findById(req.params.id)
.populate('partners')
.exec(function (err, company) {
console.log(company);
if(err) return res.send(500, err);
return res.send(204);
});
};
运行服务后我收到以下错误。有人可以帮忙吗?
{"message":"Cast to ObjectId failed for value \"[object Object]\" at path \"_id\"","name":"CastError","type":"ObjectId","value":{"id":"56dfa20249c25b7a3290596e"},"path":"_id"}
答案 0 :(得分:0)
我想你的req.params.id 是一个对象控制台log req.params.id并从该对象中找到id
或
合作伙伴:[{type:Schema.Types.Mixed,ref:' TradingPartners'}]更改为
合作伙伴:[{type:Schema.Types.ObjectId,ref:' TradingPartners'}]