Reputation: 95
I'm having a problem when creating a row and later on selecting it while still under the same transaction.
Whenever I execute the following code I usually get an empty array and rarely get an array with the newly created row.
This is a sample of the code I run:
return seq.transaction((tx) => {
const options = {transaction: tx};
return model.create(user, options)
.then(() => {
return model.findAll({
include: {
all: true
}
}, options)
.then((data) => ({
console.log(data);
});
});
Is there something basic I'm missing here? Thanks!
Upvotes: 2
Views: 1471
Reputation: 17
Pass the transaction which you had used at the time of insertion(or create) as:
const models = await connectToDatabase();
return models.model.findAll({
include: {
all: true
}
transaction: transaction
});
Upvotes: 0
Reputation: 95
I solved the problem. What I did wrong was giving the transaction to the findAll() function as an additional argument instead of part of the first argument's JSON.
So instead of:
return model.findAll({
include: {
all: true
}
}, options)
I should've done:
return model.findAll({
include: {
all: true
},
transaction: options.transaction
});
Hope this solution will be helpful for other people.
Upvotes: 6