[英]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.