HasMony关系在使用mongodb数据库的Adonis框架中不起作用

时间:2019-11-09 09:18:39

标签: node.js mongodb adonis.js

就我而言,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": []
        }

当我申请的人有很多关系时,它不会在数组中显示联系人列表

1 个答案:

答案 0 :(得分:0)

尝试像这样更改User模型:

...
contacts() {
    return this.hasMany('App/Models/Contact', '_id', 'contact_ids')
}
...