I'm trying to write my database-data to a JSON. It works when I do not use the loop.
Then I get: TypeError: first argument must be a string or Buffer
I'm using the mysql npm package.
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. 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. By default the encoding is '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;
in that initial if
block so you don't continue to the loop when an error occurs.
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.