gpt4 book ai didi

mysql - 无法解析从 MySQL 返回的 JSON

转载 作者:行者123 更新时间:2023-11-28 23:07:19 25 4
gpt4 key购买 nike

我有一个 Node.js 应用程序正在将数据写入 MySQL 后端。一个字段是我字符串化的数组。我可以在工作台中看到数据库中的数据是正确的。但是,当我检索它时,我在尝试解析它时遇到错误。

"Unexpected token o in JSON at position 1"

如果我记录结果,它显示为 [Object Object]

根据我在网上看到的,这意味着它已经是一个 JS 对象,我不需要解析它。但是我找不到任何关于如何访问数据的信息。

process: function (bot, msg, suffix) {

var ftcmds = suffix.split(" ", 1);
var ftName = ftcmds[0];
var ftArray;
var selectSQL = "SELECT FireTeam FROM fireteam WHERE Name = '" + ftName + "'";
var updateSQL = "UPDATE fireteam SET FireTeam = '" + ftArray + "'WHERE Name = '" + ftName + "'";

mysqlcon.query(selectSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);
if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
console.log("Recovered result " + result);

ftArray = result;
console.log("Attempting to update array");

ftArray.push(msg.author.id);
console.log("updated array " + ftArray);

var jsonArray = JSON.stringify(ftArray);
mysqlcon.query(updateSQL, function (err, result) {
console.log("Result |" + result);
console.log("Error |" + err);

if (err.toString().indexOf(dupErr) != -1) {
msg.author.send("Could not find that fireteam");
console.log("Error: Did not locate the requested name " + msg.author)
} else if (err) {
console.log("Caught Error " + err + " " + msg.author);
}
else {
msg.author.send("You have joined Fireteam " + name + ". I will setup a group chat on " + date + " if your team fills up.");
}
})
}
});
}

最佳答案

你应该能够将它作为一个对象来访问,所以如果结果有字段名称和标题,你可以通过以下方式访问它们:

var name = result.name
var title = result.title

关于mysql - 无法解析从 MySQL 返回的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46799002/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com