简体   繁体   English

Express JS TypeError:无法读取未定义的属性(读取“0”)SQL 查询错误

[英]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).我试过 async 和 await 但它对我不起作用(说实话,我也不确定我是否正确使用了它)。

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.每个 SQL 查询都是一个承诺,这意味着当您插入或要求数据库为您获取(选择)数据时,服务器需要一些时间来执行您的查询。 Since NodeJs has synchronous nature, it goes to execute the next line of the code before you get the result from the database.由于 NodeJs 具有同步特性,它会在您从数据库中获取结果之前执行下一行代码。 Therefore, you got undefined .因此,您得到了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 .我不太明白select语句的用途,但是如果您想知道插入行的id ,SQL 会为您返回它作为result

So, if you wish to you javascript asynchronously, the query would look like this:因此,如果您希望异步使用 javascript,查询将如下所示:

//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);
      //});
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:无法读取未定义的属性(读取“查询”) - TypeError: Cannot read properties of undefined (reading 'query') TypeError:无法读取未定义的属性(读取“原型”)React Typescript + Express - TypeError: Cannot read properties of undefined (reading 'prototype') React Typescript + Express express/tsyringe 类型错误:无法读取未定义的属性(读取“服务”) - express/tsyringe TypeError: Cannot read properties of undefined (reading 'service') 类型错误:无法读取未定义的属性(读取“原型”)。 Next.Js 和 Express - TypeError: Cannot read properties of undefined (reading 'prototype'). Next.Js and Express express.js 中缺少“/”字符 HTTP.get() 请求返回 TypeError:无法读取未定义的属性(读取“0”) - lack of "/" character in express.js HTTP .get() request returning TypeError: Cannot read properties of undefined (reading '0') TypeError:无法读取未定义(读取“过滤器”)和未定义错误的属性 - React - TypeError: Cannot read properties of undefined (reading 'filter') and undefined error - React 未定义错误:TypeError:无法读取未定义的属性(读取“图标”) - Undefined Error: TypeError: Cannot read properties of undefined (reading 'Icon') 错误无法读取未定义的属性(读取“配置”)。 TypeError:无法读取未定义的属性(读取“配置”) - ERROR Cannot read properties of undefined (reading 'configurations'). TypeError: Cannot read properties of undefined (reading 'configurations') Sequelize,Express JS - 无法读取未定义的属性(读取“findAll”) - Sequelize, Express JS - Cannot read properties of undefined (reading 'findAll') Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'query') - Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'query')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM