我有一个客户和预算之间的联系如下:
//Client.js
module.exports = {
primaryKey: 'id',
attributes: {
id: {
type: 'number',
unique: true,
autoIncrement: true
},
name: {
type: 'string'
},
phone: {
type: 'string',
unique: true
},
email: {
type: 'string',
unique: true
},
budgets: {
collection: 'budget',
via: 'client'
},
}
};
// Budget.js
module.exports = {
primaryKey: 'id',
attributes: {
id: {
type: 'number',
unique: true,
autoIncrement: true
},
client: {
model: 'client'
},
budgetItems: {
type: 'json'
}
}
};所以,POST是为这两个实体工作的,所以我可以创建这两个实体,但是:
GET /budget/1返回关联客户端的预算和id。
GET /budget/1/client返回客户机id而不填充(正如我在文档中看到的那样,应该填充它)。
GET /client/1返回客户端属性,并且没有与预算相关的字段。
GET /client/1/budgets返回404未找到
所以我可能错过了什么?
只是生成一个方向的关联,我与官方文档和第三方示例进行了比较,我的代码看起来很好。
提前感谢!
更新:
我仍然在寻找麻烦,如果我使用--silly选项运行sails,它显示了以下可用的路径:
绑定路由::get /client/:parentid/预算策略:本地化 绑定路径: get /client/:parentid/预算策略: isauth 绑定路线::get /client/:parentid/预算蓝图:填充
但是,如果我试图访问,返回一个404 Not,控制台显示由populate.js从Sails核心代码引发的以下错误:
verbo:在
populate蓝图操作中:指定的父记录(1)没有budgets。
Update2:
使用sails控制台进行调试,我看到关联是正确生成的。给定Client.findOne({id: 1}).populate('budgets').then((client)=>{console.log(client)})时,打印客户端属性和相关预算,但仍然返回404时未找到:GET /client/1/budgets
发布于 2019-05-28 07:02:13
多亏了SailsJS团队,我们发现了这个问题,它与第三方包有关,因此不得不将它从我的项目中删除。是sails-hook-deep-orm的主人被警告了。我希望有同样问题的人会到达这个帖子。
不管怎样,谢谢大家!!
问题是可用的那里
发布于 2019-05-27 17:29:39
我已经创建了快速演示,它似乎工作对我很好。
在演示中,我使用了sails版本1.2.2和sails-磁盘作为数据库,在模型属性方面有一些细微的差异,如Client.js下面所示
module.exports = {
attributes: {
name: {
type: 'string'
},
phone: {
type: 'string',
unique: true,
required: true
},
email: {
type: 'string',
unique: true,
required: true
},
budgets: {
collection: 'Budget', // <<== B is capital here
via: 'client'
},
},
};Budget.js
module.exports = {
attributes: {
client: {
model: 'Client' // <<== C is capital here
},
budgetItems: {
type: 'json'
}
},
};如果这有帮助,请告诉我。
https://stackoverflow.com/questions/56165433
复制相似问题