0

Node.js - Using await new Promise in mysql pool connection - SyntaxError: Unexpected token new

I'm trying to use a new promise inside a pool connection ... but I'm getting this SyntaxError: Unexpected token new. What am I missing?

Error:

let rowsPromise01 = await new Promise((resolve,reject)=>{
                              ^^^
SyntaxError: Unexpected token new

js:

const pool = require('./config/dbpool');    

pool.getConnection(function(err, connection) {
  if (err) throw err;

   let rowsPromise01 = await new Promise((resolve,reject)=>{
    connection.query("SELECT * FROM table01", function(err, rows) {
        //connection.release();
        if (err) {
          console.log(err);
          reject(err);
        } else {
          console.log('Found records: '+rows.length);
          resolve(rows);       
        }
    });
   });

   let rowsPromise02 = await new Promise((resolve,reject)=>{
    connection.query(SELECT * FROM table02,function(err, results) {
      connection.release();
      if (err) {
        console.log(err);
        reject(err);
      } else {
      console.log('Found records: '+result.length);
      resolve(results);
      }
    });

    });

});

The dbpool.js looks like this:

const mysql= require('mysql');

//database connection
var pool = mysql.createPool({
  connectionLimit: 10,
  host     : 'localhost',
  user     : 'root',
  password : 'password',
  database : 'database'
});

module.exports = {
  getConnection (callback) {
    pool.getConnection((err, conn) => {
      if(err) {
        return callback(err);
      }
      callback(err, conn);
    });
  },
  query (query, params=[], cb) {
    this.getConnection((err, conn) => {
        conn.release();
        if(err) return cb(err, null);
        conn.query(query, params, (err, res) => {
            if(err) return cb(err, null);
            return cb(null, res);
        });
    });
  }
}

1 Answer 1

3

The error is a bit of a red herring, the issue is really with the await keyword, which is only valid inside functions that are marked with async, which in your case is missing.

Fix:

pool.getConnection(async function(err, connection) { .... });

More info here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function#Description

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

4 Comments

The error is gone ... and it seems that the first query is going through ... but now I'm getting this error: Error: Connection already released . Can I not call connection.release(); twice like above?
@PhilippM no, it can only be released back to the pool once (a similar issue occurs in dbpool.query, where you release the connection before even using it for a query). Take a look at pool.query(), which might be easier to use.
mmh ... I'm confused. I took the connection.release() out except in the last query but now I'm getting again { Error: read ECONNRESET at _errnoException (util.js:1022:11) at TCP.onread (net.js:615:25) for the second query which I also got when not using the pool connection
@PhilippM perhaps take a look at the MySQL logs to see why it's closing the connection.

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.