如何停止nodejs/knexjs中的异步数据库调用



所以上周我一直被这个问题困扰着。我正试图将一项训练添加到我的数据库中,我从前端收到了3件事:训练名称是一个字符串,一个名为exercises的数组,它包含该训练中每个训练的对象。最后,一个名为sets的数组,它是一个2d数组,每个数组包含每个集合的一个对象

const exercises = [
{name: ''}
]
const sets = [
[
{
reps: '',
kg: ''
}
]
]

我正在使用knex,我想将这些数据插入到我的数据库中。这是我的代码:

app.post('/newworkout', (req, res) => {
const {workoutName, exercises, sets} = req.body;
const loggedSets = [];
let haveRoutinesFinished = false;
let haveExercisesFinished = false;
PushRoutinesToDB = () => {
db('routines')
.returning('id')
.insert({
userid: user.id,
name: workoutName
})
.then(routineID => {
workoutInfo.routineID =  routineID[0];
haveRoutinesFinished = true;
console.log('1')
return db('loggedroutines')
.insert({ 
userid: user.id,
routineid: workoutInfo.routineID,
routinedate: new Date() 
})
})
}

PushExercisesToDB = () => {
exercises.map(exercise => {
console.log('2')
db('exercises')
.insert({
userid: user.id,
routineid: workoutInfo.routineID,
name: exercise.name
})
.returning('id')
.then(exerciseID => {
console.log('exercise id', exerciseID)
exerciseIDArray.push(exerciseID[0])
console.log("exerciseIDArray", exerciseIDArray)
haveExercisesFinished = true;
return db('loggedexercises')
.insert({
userid: user.id,
routineid: workoutInfo.routineID,
exerciseid: exerciseID[0]
})
})
})

}

PushSetsToDB = () => {
sets.map((setsArray, i) => {
setsArray.map(set => {
loggedSets.push({
userid: user.id,
routineid: workoutInfo.routineID,
exerciseid: exerciseIDArray[i],
reps: set.reps,
kg: set.kg
})
})
})
console.log(loggedSets)

db('loggedsets').insert(loggedSets)
}
PushRoutinesToDB()
console.log(haveRoutinesFinished)
if (haveRoutinesFinished === true) {
PushExercisesToDB()
}
if (haveExercisesFinished === true) {
PushSetsToDB()
}
})

代码本身是有效的,但由于第一个数据库调用是异步的,第二个调用不起作用,它依赖于第一个调用。我试图使用回调来解决这个问题,但这只会导致我的代码无法运行。有什么想法吗?

由于函数是Asynchronous,即使第一个函数尚未完成,线程也会继续。

使用async/await必须解决您的问题。

注意注释以查看更改

app.post('/newworkout', async (req, res) => { //asynchronous Function
const {workoutName, exercises, sets} = req.body;
const loggedSets = [];
let haveRoutinesFinished = false;
let haveExercisesFinished = false;
PushRoutinesToDB = async () => { //asynchronous Function
db('routines')
.returning('id')
.insert({
userid: user.id,
name: workoutName
})
.then(routineID => {
workoutInfo.routineID =  routineID[0];
haveRoutinesFinished = true;
console.log('1')
return db('loggedroutines')
.insert({ 
userid: user.id,
routineid: workoutInfo.routineID,
routinedate: new Date() 
})
})
}

PushExercisesToDB = async () => { //asynchronous Function
exercises.map(exercise => {
console.log('2')
db('exercises')
.insert({
userid: user.id,
routineid: workoutInfo.routineID,
name: exercise.name
})
.returning('id')
.then(exerciseID => {
console.log('exercise id', exerciseID)
exerciseIDArray.push(exerciseID[0])
console.log("exerciseIDArray", exerciseIDArray)
haveExercisesFinished = true;
return db('loggedexercises')
.insert({
userid: user.id,
routineid: workoutInfo.routineID,
exerciseid: exerciseID[0]
})
})
})

}

PushSetsToDB = async () =>  {  //asynchronous Function
sets.map((setsArray, i) => {
setsArray.map(set => {
loggedSets.push({
userid: user.id,
routineid: workoutInfo.routineID,
exerciseid: exerciseIDArray[i],
reps: set.reps,
kg: set.kg
})
})
})
console.log(loggedSets)

db('loggedsets').insert(loggedSets)
}
// Now your all functions are asynchronous You can call them in series with 'await'
await PushRoutinesToDB()
await PushExercisesToDB()
await PushSetsToDB()
})

相关内容

  • 没有找到相关文章

最新更新