我对nodejs或sql编码并不陌生,只是对使用nodejs的sql非常陌生
以下是我的代码
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "root",
password: "cFeu11qJgQ8lNxhO",
database: "users"
});message是设置聊天消息的变量
con.query("SELECT * FROM chatmessages WHERE InComingChat=", message, function (err,rows) {
rows.forEach( (row) => {
console.log("",row.InComingChat," is in ",row.OutComingChat);
});
})但它显示TypeError:无法读取未定义的属性'forEach‘,应如何写入以获取InComingChat中的值并输出值为OutComingChat中的值?
发布于 2018-07-03 12:22:26
con.query("SELECT * FROM chatmessages WHERE InComingChat=", message, function (err,rows) {
rows.forEach( (row) => {
console.log("",row.InComingChat," is in ",row.OutComingChat);
});
})这里的
rows是一个数组,查询中的每个语句都有一个元素:
尝尝这个
con.query("SELECT * FROM chatmessages WHERE InComingChat=", message, function (err,rows) {
rows[0].forEach( (row) => {
console.log("",row.InComingChat," is in ",row.OutComingChat);
});
})read this -基于查询,这是他们定义的结构。
connection.query('SELECT 1; SELECT 2', function (error, results, fields) {
if (error) throw error;
// `results` is an array with one element for every statement in the query:
console.log(results[0]); // [{1: 1}]
console.log(results[1]); // [{2: 2}]
});https://stackoverflow.com/questions/51146431
复制相似问题