3

I have a Node.js script that used to work, but after I switched to another VM it does not work anymore. Can anyone see what the problem is? Here is the function, db is a database:

this.start = function() {
  logger.debug('Starting up.');
  db.serialize(() => {
    db.run("DELETE FROM jobs WHERE status = 'failed'")
      .run("UPDATE jobs SET status = 'queued'", (err) => {
        if (err) {
          logger.error(err.message);
        } else {
          logger.info('done');
        }
      });
  });
}

Now I get the following error:

TypeError: Cannot read property 'run' of undefined
 at Database.db.serialize ()
 at TransactionDatabase.serialize
 at module.exports.start
 at Object.<anonymous>
...

The error is pointing at the second ".run".

My Node.js version is 10.4.1, sqlite3 version 3.8.2.

What am I missing? Some module?

2
  • Which .run throws the error ? Maybe you cannot chain when serializing the queries Commented Jul 3, 2018 at 14:43
  • It was the second .run. I thought that on the other system this script worked. In the end I reworked the script to get rid of chaining. Works now, but shame not to know what the problem was. Commented Jul 5, 2018 at 7:49

1 Answer 1

0

I think I found the answer. Chaining run() runs the queries nearly at the same time. According to this answer, the function run() starts the query, but returns immediately.

However, if you serialize and chain, those two methods cannot be used at the same time. You are trying run queries sequentialy, but also at the same time.

Although, depending on your needs, you can nest serialize, parallelize or callbacks, as shown in the "control flow" doc.

I guess the method serialize() "locks" chaining by changing the return value of run() to undefined.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply, unfortunately, I cannot confirm if this is right anymore, but might be useful for someone else!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.