就我而言,hasMany关系不起作用 像
这样的数据库 Users.js
{
"_id" : ObjectId("5dc5617ac22c2921fc32b1f0"),
"name" : "vikas",
"email" : "vikas@gmail.com",
"contact_ids" : [
"5dc5572ee6143821e43ece31"
],
"created_at" : ISODate("2019-11-08T12:37:12.452Z"),
"updated_at" : ISODate("2019-11-08T12:37:12.456Z")
}
Contact.js
{
"_id" : ObjectId("5dc5572ee6143821e43ece31"),
"name" : "vikas Contacts",
"mobile" : 95754681658,
"user_id" : "5dc5617ac22c2921fc32b1f0",
"created_at" : ISODate("2019-11-08T11:53:15.781Z"),
"updated_at" : ISODate("2019-11-08T11:53:15.786Z")
}
我正在将关系应用于USERS模型
User.js
/** get many contacts */
contacts() {
return this.belongsToMany('App/Models/Contact', '_id', 'contact_ids')
// hasMany(relatedModel, primaryKey, foreignKey)
}
最终结果是//输出
{
"_id": "5dc5617ac22c2921fc32b1f0",
"name": "vikas",
"email": "vikas@gmail.com",
"contact_ids": [
"5dc5572ee6143821e43ece31"
],
"created_at": "2019-11-08T12:37:12.452Z",
"updated_at": "2019-11-08T12:37:12.456Z",
"contacts": []
}
当我申请的人有很多关系时,它不会在数组中显示联系人列表
答案 0 :(得分:0)
尝试像这样更改User
模型:
...
contacts() {
return this.hasMany('App/Models/Contact', '_id', 'contact_ids')
}
...