我在最後幾個小時撞到了我的頭,我無法弄清楚我的問題的解決方案。在我的風帆模型中,我有2個一對多的關聯。 'A'模型可以有許多'B','B'模型可以有許多'C'。在我的控制器中,當我執行a.find().populate('b') (...)
時,它會返回A模型的整個集合,但只填充一個B的A模型的每個條目,這與我的數據庫(mysql)中的當前數據集不匹配。並且不填充B模型中的C條目。換句話說,我試圖實現類似嵌套人口。
控制器代碼有問題,對吧?我該如何做這項工作? 在此先感謝!風帆/水線填充不能按預期工作
編輯:
Company.js
module.exports = {
identity: 'company',
attributes: {
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
nif: {
type: 'integer',
required: true,
minLength: 9
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
required: true
},
email: {
type: 'email',
required: true
},
facilities: {
collection: 'facility',
references: 'facility',
on: 'id',
via: 'company'
}
}
};
Facility.js
module.exports = {
identity: 'facility',
attributes: {
company: {
columnName: 'id_company',
model: 'company'
},
warehouses: {
collection: 'warehouse',
references: 'warehouse',
on: 'id',
via: 'facility'
},
name: {
type: 'string',
required: true
},
address: {
type: 'string',
required: true
},
zip_code: {
type: 'string',
required: true
},
city: {
type: 'string',
required: true
},
country: {
type: 'string',
required: true
},
phone_number: {
type: 'string',
},
email: {
type: 'email',
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
Warehouse.js
module.exports = {
identity: 'warehouse',
attributes: {
facility: {
columnName: 'id_facility',
model: 'facility',
},
name: {
type: 'string',
required: true
},
longitude: {
type: 'float',
},
latitude: {
type: 'float'
}
}
};
MainControl LER的相關代碼:
companies: function(req, res) {
company.find().populate('facilities').exec(function(err, comp){
var error = '';
if(err){
error = 'Unable to retrieve the requested information. Try again later and, if the problem persists, contact the platform administrator.';
} else if(!comp[0]) {
error = 'There\'s no company data inserted.';
}
// (...)
});
},
你能分享你的模型和控制器代碼? – irobert91 2015-02-10 11:18:44
@RobertIgnat謝謝你的回覆。我爲這個問題添加了一些相關的代碼。 – 2015-02-10 12:10:17
首先,Waterline目前不支持嵌套人口,所以'Company.find()。populate('facilities')'只會填充每個公司的設施,而不是每個設施的倉庫。 其次,你有沒有嘗試刪除模型中的'references'和'on'?如果是的,你會得到相同的行爲,你能舉出一個反應的例子嗎? – irobert91 2015-02-10 16:27:38