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?

user7637745
  • 965
  • 2
  • 14
  • 27
Monday to Friday
  • 239
  • 5
  • 16
  • Which `.run` throws the error ? Maybe you cannot chain when serializing the queries – Seblor Jul 03 '18 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. – Monday to Friday Jul 05 '18 at 07:49

1 Answers1

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.

Seblor
  • 6,947
  • 1
  • 25
  • 46