我想使用 async/await 运行多个函数,包括 for 循环。该循环还需要相互运行才能完成。目前我正在使用此功能,但似乎无法正常工作。
对于 for 循环查询,我需要在第一个查询中创建的 id 来执行它。
const createStudent = async (studentObj) => {
try {
await database.action(async () => {
const newStudent = await studentCollection.create(student => {
student.name = studentObj.name
student.age = studentObj.age
})
for (let contactObj of studentObj.contacts) {
try {
await contactCollections.create(contact => {
contact.student_id = newStudent.id
contact.type = contactObj.type
contact.contact = contactObj.contact
})
} catch (error) {
console.log(error);
}
}
})
} catch (error) {
console.log(error);
}
}
目前我收到错误
[WatermelonDB] The action you're trying to perform (unnamed) can't be performed yet, because there are 2 actions in the queue.
Current action: unnamed. Ignore this message if everything is working fine.
But if your actions are not running, it's because the current action is stuck.
Remember that if you're calling an action from an action, you must use subAction(). See docs for more details.
红糖糍粑
相关分类