trying to get the resultId back from an insert in react.js using mysql2/promise

22 views Asked by At

I am trying to get the resultId back when using mysql2/promise, but it won't allow be to do a

callback on the connection.query line, so I have to use:
      (async () => {
            try {
                                 //connection.query(config, function (error, results, fields) {
              const rows = await connection.query(sql);
              //console.log("results to resultId is:" + results.resultId);
              console.log(rows);
            } finally {
              
              //connection.end();
            }
          })()  


here is my require section:

const PORT = 8000;
const express = require('express');
const cors = require('cors');
const mysql = require("mysql2/promise");

and my connection to database:

  async function getConnection() {
    const connection = await mysql.createConnection({
        host: 'localhost',
        user: 'root',
        password: 'thepasswordforexample',
        database: 'databasename',
    });
    return connection;
  }

I can run the query, but I don't get the resultId back. How do I fix this? Any help would be greatly appreciated. I already looked through: node.js async/await using with MySQL and none of those solutions work.

Thanks in advance, and please bear with me as I am learning this new language.

0

There are 0 answers