【问题标题】:AdonisJS Associate with multiple oneToMany relationshipsAdonisJS 关联多个 oneToMany 关系
【发布时间】:2019-03-14 22:36:50
【问题描述】:

如何在 Adonis 中关联 oneToMany 关系中的值数组。

文档显示以下内容以关联单个值

const Profile = use('App/Models/Profile')
const User = use('App/Models/User')

const user = await User.find(1)
const profile = await Profile.find(1)

await profile.user().associate(user)

如果我的表单提交了一个包含多个用户 ID 的数组怎么办 我知道我可以使用 array.map 并循环遍历每一个,但这是一个异步命令,我的控制器会在 map 完成之前尝试响应客户端。

users.map(async (u)=>{
  let user = await User.find(u)
  await profile.user().associate(user)
})

return //I think this would return before the above map function completed.

【问题讨论】:

    标签: javascript arrays adonis.js associate


    【解决方案1】:

    你可以用 for of 来做到这一点

    for (const u of users) {
        const user = await User.find(u)
        await profile.user().associate(user)
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多