I have a following function, which works, when passed a new user data . it saves the object and the child object into the mysql table successfully. but how do i return the object back , once saved to the database, given i'm using sequelize transaction.
static async add(user) {
let transaction;
try {
// get transaction
transaction = await models.sequelize.transaction();
// *****how to return the newly created user *****************************************
models.User.create(user).then(newUser => {
const id = newUser.id;
//save address
if(user.address){
address.userId = id;
models.Address.create(address);
}
}).catch(error => {
throw error;
});
await transaction.commit();
} catch (error) {
console.log(error);
// Rollback transaction
if (transaction) await transaction.rollback();
throw error;
}
}