Reputation: 4771
In Express.js I am trying to do the following using the serialization of two queries where the second one depends on the first one:
As far as I understand, the second query is called before the first one completes. Also the "console.log("My entry: "+data.entryID);" returns the right entryID. Below I have shown the code I am using as well as the error I get. Any help would be appreciated.
db.serialize(function(){
db.get("Select entryID from entry where pID=$pID",{$pID:participName},function(err, data){
if(err) throw err;
foundID=data.entryID;
console.log("My entry: "+data.entryID);
});
db.run("insert into observation(entryID, objID, data) values($entryID,1,$intro_counter)", {$entryID:foundID, $intro_counter:intro_counter});
});
events.js:71
throw arguments[1]; // Unhandled 'error' event
^
Error: SQLITE_CONSTRAINT: foreign key constraint failed
Upvotes: 1
Views: 987
Reputation: 180070
Your problem is that db.get
itself might be serialized, but the callback you give to db.get
is still called asynchronously.
To solve this, move the db.run
call into the callback.
Upvotes: 1