node.js - HasMany relation not working in Adonis framework using mongodb database
问题描述
In my case, hasMany relation not working database like
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")
}
and I'm applying relation on USERS model
User.js
/** get many contacts */
contacts() {
return this.belongsToMany('App/Models/Contact', '_id', 'contact_ids')
// hasMany(relatedModel, primaryKey, foreignKey)
}
and the final result is // OUTPUT
{
"_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": []
}
when I applying has many relations it doesn't show contacts list in an array
解决方案
尝试更改您的User
模型,例如:
...
contacts() {
return this.hasMany('App/Models/Contact', '_id', 'contact_ids')
}
...
推荐阅读
- r - 在读取 sas 文件以触发或将数据拉到 r 会话时,有没有办法设置字符编码?
- python-3.x - 从停用词中清除多个术语
- python - tf.keras.layers.Conv3DTranspose 在输入动态形状时抛出 TypeError
- html - 如何在 Symfony 4 表单中的输入上添加占位符?
- python - 删除对象而不刷新django
- amazon-web-services - 对 S3 存储桶中新对象的 PUTS 写后读一致性的警告
- javascript - 当输入、选择等形式发生变化时如何获取计数器?
- node.js - 如何在线正确上传 localhost 网站?
- powershell - 如何比较文件中的两个连续字符串
- cordova - Ionic 4 不能在没有 cordova 的情况下使用 HTTP 插件了吗?