简体   繁体   中英

Express JS TypeError: Cannot read properties of undefined (reading '0') SQL query error

I am trying to Insert something in my db and then access to it directly after, but because node is async, this somehow does not work as planed. But I know that there must be a way to get this going. Heres my code:

router.post('/anzeigeerstellen', upload_inserat.single('Anzeigebild'), function (req, res) {
  let titel = req.body.TitelderAnzeige,
      beschreibung = req.body.Beschreibung,
      inbild = req.file.filename,
      ses = req.session;

    
    pool.query("INSERT INTO inserat (titel, beschreibung, inseratbildname, email)
    VALUES (?, ?, ?, ?)",
    [titel, beschreibung, inbild, ses.email],
    (error, results, fields) => {
      pool.query("SELECT iid FROM inserat WHERE titel = ?, beschreibung = ?,
      inseratbildname = ?, email  = ?",
      [titel, beschreibung, inbild, ses.email],
      (error, results, fields) => {
          res.redirect('anzeige?id=' + results[0].iid);
      });
    });
});

And the error is the following:

TypeError: Cannot read properties of undefined (reading '0')

I've tried async and await but it didn't work for me (and I'm also not sure if I used it right, to be honest).

Every SQL query is a promise which means when you insert or ask the database to get (select) data for you it takes some time for the server to execute your query. Since NodeJs has synchronous nature, it goes to execute the next line of the code before you get the result from the database. Therefore, you got undefined .

I didn't quite understand the purpose of the select statement but if you wanted to know the id of the inserted row, the SQL returns it for you as a result .

So, if you wish to you javascript asynchronously, the query would look like this:

//Setting the route asynchronous
router.post('/anzeigeerstellen', upload_inserat.single('Anzeigebild'), async (req, res)=> {
  let titel = req.body.TitelderAnzeige,
      beschreibung = req.body.Beschreibung,
      inbild = req.file.filename,
      ses = req.session;

    //Awaiting until the db resolves
    await pool.query("INSERT INTO inserat (titel, beschreibung, inseratbildname, email)
    VALUES (?, ?, ?, ?)", [titel, beschreibung, inbild, ses.email],
    (err, results, fields) => {
      //Always check if there is an error
      if (err) console.error(err)
      else console.log(results) //results should be the id of the row or the row itself
      /*pool.query("SELECT iid FROM inserat WHERE titel = ?, beschreibung = ?,
      inseratbildname = ?, email  = ?",
      [titel, beschreibung, inbild, ses.email],
      (error, results, fields) => { There is no need for this query since the results of the upper query is the row you wanted*/
          res.redirect('anzeige?id=' + results[0].iid);
      //});
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM