javascript - AdonisJS 关联多个 oneToMany 关系
问题描述
如何在 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 并遍历每个用户 ID,但这是一个异步命令,我的控制器会在 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.
解决方案
你可以用 for 来做到这一点
for (const u of users) {
const user = await User.find(u)
await profile.user().associate(user)
}
推荐阅读
- java - Java 通过 JDBC 连接到 Postgres - SQLException
- amazon-redshift - 红移窗口函数中的条件和最大参考
- user-interface - 我应该从哪个屏幕尺寸开始制作线框?
- css - 为全日历中的外部事件重置(左上角宽度 css 样式)
- nginx - 重定向到 root,即使查询为空
- react-native - 执行 FLATLIST 时的错误是什么?
- apache-camel - apache camel中的sftp尝试kerberos身份验证
- php - Codeigniter facebook登录未获取用户数据
- amazon-s3 - SageMaker GetBucketLocation 操作:拒绝访问
- python - 根据条件将列值转换为列表列