Handling Errors in add_question Query with Try-Catch Block in JavaScript
To check if the `add_question` query failed to run, you can use a try-catch block and catch the error thrown by the `questions.create` function. Here's an example of how you can modify the code to handle the error:
const add_question = async (question, options, iscorrect, accestoken) => {
try {
const add_question = await questions.create({
question_id: uuidv4(),
question: question,
options: options,
iscorrect: iscorrect,
created_at: new Date()
})
return {
add_question
}
} catch (error) {
const responseData = responseHandler(
false,
response_message.add_question.error
);
return {
responseData
}
}
}
In the catch block, you can handle the error by returning an object with the `responseData` property set to the error message or any other value you want to return when the query fails to run.
原文地址: https://www.cveoy.top/t/topic/pMN1 著作权归作者所有。请勿转载和采集!