db.query("INSERT INTO `chat_entries`(`author`, `authorRank`, `receiver`, `timestamp`, `message`) VALUES (" + db.escape(GetUsername(connection)) + ", " + GetUserData(GetUsername(connection), "rank") + ", '-', " + Math.round(new Date().getTime() / 1000) + ", '" + db.escape(messageData.message) + "')", function(result, rows){
console.log(result.insertId);
});
控制台告诉我:
Cannot read property 'insertId' of null
在我搜索解决方案后,我发现了此页面: https://github.com/mysqljs/mysql#getting-the-id-of-an-inserted-row
根据此文档,它必须有效。 我的错误在哪里? 提前致谢。
最佳答案
你需要更换
console.log(result.insertId);
与
console.log(rows.insertId);
因为它是您传递给回调函数的变量。
关于javascript - node.js & mysql - 如何获取最后插入的 ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38186555/