0

I have this code:

const conn = mysql.createConnection(connData);

conn.connect(function (err) {
    if (err) throw err;

    function query(sql) {
        return new Promise(function (resolve, reject) {
            if (err) reject(err);
            conn.query(sql, function (err, result) {
                if (err) reject(err);
                resolve(result);
            });
        });
    }

    function createResource(name, startingPrice) {
        query("SELECT * FROM resources WHERE name = '" + name + "'").then(function (result) {
            if(result.length > 0) {
                console.log("Resource already created");
                return;
            }

            query("INSERT INTO resources (name) VALUES ('" + name + "')");
            query("SELECT * FROM resources WHERE name = '" + name + "'").then(function(result) {
                console.log(result);
            });
            return;
        });

        return;
    }

    createResource("resource1", 100);

    conn.end();
});

When I try to run it it throws an error:

Cannot enqueue Query after invoking quit; 
code: 'PROTOCOL_ENQUEUE_AFTER_QUIT', fatal: false

I think it is because of the SELECT query into the other SELECT query, in the createResource function. What is wrong and how can i fix it?

3
  • createResource is a Promise that you forget to wait for resolution. Hence you close the connection before it even starts selecting data. Commented May 5, 2024 at 14:23
  • You are not waiting for any asynchronous function. Even query needs an await Commented May 5, 2024 at 14:32
  • Does this answer your question? Cannot enqueue Query after invoking quit Commented May 6, 2024 at 0:13

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.