我创建了以下函数来将锻炼插入数据库。
const date = new Date().getDate();
let id;
await db.transaction( tx => {
tx.executeSql( 'INSERT INTO workouts (name, created_at) values (?, ?)',
[name, date]
,(transact,resultset) => {id = resultset.insertId, console.log('we made it', id )}
,(transact,err) => {console.log('We have encounter an Error', err)}
);
});
//it does not return the id
console.log("id before return" + id)
return id;
}
我想返回刚插入的行的id,但它不起作用,返回undefined。但是行console.log('we made it',id)}确实正确地打印了id。该函数从以下代码调用:
const handleAddWorkoutModal = async (workoutName, setShowAddWorkoutModal, router) => {
try {
const id = database.insertWorkout(workoutName);
console.log(id);
setShowAddWorkoutModal(false);
router.push({
pathname: '/pages/workouts/createWorkout/CreateWorkout',
params: { workoutName: workoutName, workoutId: id}
});
} catch (e) {
console.warn(e);
}
}
export default handleAddWorkoutModal;
我也试过使用SQLite的RETURNING语句,但它在ATM上有一些问题。
https://github.com/expo/expo/issues/24283
你能帮帮我吗?
1条答案
按热度按时间hxzsmxv21#
问题是我必须使用一个promise来让await正常工作。
.then用于获取插入中函数调用的值。