[英]TypeError while iterating over an loop in NodeJS
I'm trying to write my database-data to a JSON. 我正在尝试将数据库数据写入JSON。 It works when I do not use the loop.
当我不使用循环时,它起作用。
Then I get: TypeError: first argument must be a string or Buffer 然后我得到: TypeError:第一个参数必须是字符串或Buffer
I'm using the mysql npm package. 我正在使用mysql npm软件包。
con.query('SELECT * FROM mydb.orders_view;', function(err, rows, fields) {
if (err) {
res.status(500).json({ result: 'Error' })
}
for (var i = 0; i < rows.length; i++) {
res.write(
{
time: date,
Order: [{
OrderedBy: rows[i]['OrderedBy']
}]
}
);
}
res.end();
});
});
You're calling write
with an object. 您正在使用对象调用
write
。 According to the documentation, it must be a string or buffer: 根据文档,它必须是字符串或缓冲区:
chunk can be a string or a buffer.
块可以是字符串或缓冲区。 If chunk is a string, the second parameter specifies how to encode it into a byte stream.
如果chunk是字符串,则第二个参数指定如何将其编码为字节流。 By default the encoding is 'utf8'.
默认情况下,编码为“ utf8”。 The last parameter callback will be called when this chunk of data is flushed.
刷新此数据块时将调用最后一个参数回调。
Side note: You're not handling the error case properly, you probably want a return;
旁注:您没有正确处理错误情况,您可能希望
return;
in that initial if
block so you don't continue to the loop when an error occurs. 在该初始
if
块中,这样您就不会在发生错误时继续循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.