[英]How to convert mysql row of data into a javascript array? Node.js app
首先,我是新手。 毫无疑问,我犯了一些简单的错误。
结合使用Node.js和MySQL数据库,我正在构建一个允许用户登录的基本Web应用程序。 他们登录后,将被带到个人资料页面,并以条形图的形式显示他们完成的测验结果。
我想将一行mysql数据隐藏到一个数组中。
const mysql = require('mysql');
const dbconfig = require('/config/database');
const connection = mysql.createConnection(dbconfig.connection);
connection.query('USE ' + dbconfig.database);
// Create an array of scores for each category depedning on the user who's
// loggedin.
var category1scoreQuery =
"SELECT c1q1, c1q2, c1q3, c1q4, c1q5, c1q6, c1q7, c1q8
FROM nodejs_login.assessment_score
AS a JOIN users as u ON a.respondent_id = u.user_respondent_id
WHERE a.respondent_id = user.user_respondent_id;";
connection.connect(function(err){
if (err) throw err;
connection.query(category1scoreQuery, function(err, result, fields) {
if (err) throw err;
Object.keys(result).forEach(function(key){
var cat1Array = result[key];
// want to return array e.g. ["45/60", "60/60", "40/40","30/40","15/20",
// "30/40", "30/60", "20/40"];
console.log(cat1Array);
})
})
});
// I want to convert it to an array to parse the array of strings into
// totalUserScore over maxCategoryScore
var i;
var userCategoryScore1 = 0;
var maxCategoryScore = 0;
for(i=0; i < cat1Array.length;i++){
var splitScore = cat1Array[i].split("/");
console.log(splitScore);
myQuestionScore = parseInt(splitScore[0], 10);
userCategoryScore1 += myQuestionScore;
console.log(userCategoryScore);
maxQuestionScore = parseInt(splitScore[1]);
maxCategoryScore = maxCategoryScore + maxQuestionScore;
console.log(maxCategoryScore);
}
这就是我实际上得到的,不允许我循环通过。
RowDataPacket {
c1q1:“ 15/60”,
c1q2:“ 15/60”,
c1q3:“ 10/40”,
c1q4:“ 10/40”,
c1q5:“ 5/20”,
c1q6:“ 10/40”,
c1q7:“ 15/60”,
c1q8:'10 / 40'}
这应该为您工作:
const RowDataPacket= {
c1q1: '15/60',
c1q2: '15/60',
c1q3: '10/40',
c1q4: '10/40',
c1q5: '5/20',
c1q6: '10/40',
c1q7: '15/60',
c1q8: '10/40' }
const values=Object.values(RowDataPacket);
console.log(values)
参考资料[第一部分]: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_objects/Object/values
说明:Object.values()方法返回给定对象自己的可枚举属性值的数组,其顺序与for ... in循环所提供的顺序相同(区别在于for-in循环枚举了in ...原型链)。
对于第二部分,要计算总得分://使用第一部分中的values数组
const scores=values.reduce((accum,value)=>{
const splitValues=value.split('/')
return {
score:accum.score + parseInt(splitValues[0]),
maxScore:accum.maxScore + parseInt(splitValues[1]),
}
},{score:0,maxScore:0})
console.log(scores)
参考[第二部分]: https : //developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce
说明:reduce()方法在数组的每个元素上执行reducer函数(由您提供),从而产生单个输出值。
通过javascript与数据库进行交互时,这是一个相当普遍的问题。 要获得所需的内容,可以尝试使用JSON库,如下所示:
usersRows = JSON.parse(JSON.stringify(result));
同样,这与您的问题并不完全相关,但这使我的工作变得轻松很多:考虑使用节点Promisify模块将您的查询转换为Promise (来自此处: https://www.npmjs。 com / package / util-promisify )。 这样,您不必像执行代码那样使用回调函数,而是像这样:
var results = await connection.query(category1scoreQuery);
//Process results here
同样,这只是一个建议,但我发现它很有用。
干杯!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.