MySQL 如何解析node.js、express.js、mysql2中“rows”对象的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22381998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to parse the data from "rows" object in node.js,express.js,mysql2
提问by jaassi
I m using the node,express,mysql2 packages .When i m using console.log(rows) ,it is giving me following output:
我正在使用 node,express,mysql2 包。当我使用 console.log(rows) 时,它给了我以下输出:
[{"userid": "test","password": "test"}]
And here is my Code :
这是我的代码:
var application_root = __dirname,
express = require("express"),
mysql = require('mysql2');
path = require("path");
var app = express();
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : '123',
database: "bbsbec"
});
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
connection.query('SELECT * from pass', function(err, rows) {
res.json(rows);
console.log(rows);
});
I just want to know that how can i parse this "rows" object so that i can retrive both userid and password .
我只想知道如何解析这个“行”对象,以便我可以检索 userid 和 password 。
回答by TimWolla
[{"userid": "test","password": "test"}]
This is an Array
of Object
s. So: First loop over the array to get a single object and then extract it's properties:
这是一个Array
的Object
秒。所以:首先遍历数组以获取单个对象,然后提取它的属性:
for (var i = 0; i < rows.length; i++) {
var row = rows[i];
console.log(row.userid);
}
回答by Tobi
Try this (this is really basic):
试试这个(这是非常基本的):
connection.query('SELECT * from pass', function(err, rows) {
res.json(rows);
var user = rows[0].userid;
var password= rows[0].password;
});
回答by Gtm
connection.query('SELECT * from pass', function(err, rows) {
data = rows[0];
let user = data.userid;
let password= data.password;
res.json(rows);
});