bookUnitQuestionOption

bookUnitQuestionOption

我需要进行交易:

首先,我创建了一个新的bookUnitQuestionOption,创建后,我需要使用保存的ID更新其他表(bookUnitQuestion)。

我尝试类似的东西:

async store ({ request }){
     const bookUnitQuestionOption = {...request.all(), user_id: request.user_id};
     const trx = await Database.beginTransaction()
     const responseInsercaoOption = BookUnitQuestionOption.create(bookUnitQuestionOption, trx)
     if(bookUnitQuestionOption.correct === true)
            await this.atualizaOptionCorreta(bookUnitQuestionOption.book_unit_question_id, responseInsercaoOption.id, trx)

     trx.commit()
}

async atualizaOptionCorreta(bookUnitQuestionId, idOpcaoPergunta, transaction){
    await BookUnitQuestion.query()
        .where('id', bookUnitQuestionId)
        .update({correct_answer_description_id :idOpcaoPergunta}, transaction);
}


但我收到500错误:


  消息:“事务查询已完成,使用DEBUG = knex:tx运行
  有关更多信息”

最佳答案

在第一个查询之前,您缺少等待,第二个查询被跳过,因为在这种情况下,如果if和trx.commit()在执行第一个查询之前就已运行。

const responseInsercaoOption = BookUnitQuestionOption.create(bookUnitQuestionOption, trx)


应该:

const responseInsercaoOption = await BookUnitQuestionOption.create(bookUnitQuestionOption, trx)

10-06 04:48