亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶 for 循環的多個 Async Await

帶 for 循環的多個 Async Await

手掌心 2023-07-20 10:09:10
我想使用 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.
查看完整描述

1 回答

?
紅糖糍粑

TA貢獻1815條經驗 獲得超6個贊

是的你是對的!。它不能與多個數據庫創建記錄請求一起正常工作。

每當您在一項操作中進行多個更改(創建、刪除或更新記錄)時,您應該對它們進行批處理。

你可以這樣做:

const batchActions = [];


for (let contactObj of studentObj.contacts) {

? batchActions.push(

? ? contactCollections.prepareCreate(contact => {

? ? ? contact.student_id = newStudent.id

? ? ? contact.type = contactObj.type

? ? ? contact.contact = contactObj.contact

? ? })

? )

}


database.batch(batchActions)


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 220 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號