【问题标题】:MySQL query return [Object, object] in NodeJS, why?MySQL查询在NodeJS中返回[Object,object],为什么?
【发布时间】:2017-08-02 18:01:56
【问题描述】:

这是我的简单陈述:

var mysql = require("mysql");
var CONCDB = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "1234",
  database: "pwc"
})

CONCDB.connect();

CONCDB.query("SELECT first_name FROM users LIMIT 1", function(err, rows, fields) {
  if (err) throw err;
    console.log("The solution is: ", rows);
});

为什么我会得到这样的结果:

[ RowDataPacket { first_name: <Buffer 47 61 62 72 69 65 6c> } ]

如果我使用

... function(err, results) {
  ...
  console.log("The solution is: ", results)

我得到的和使用行一样。

使用函数(错误、行、字段)并返回字段我得到以下信息:

[ FieldPacket {
  catalog: 'def',
  db: 'pwc',
  table: 'users',
  orgTable: 'users',
  name: 'first_name',
  orgName: 'first_name',
  charsetNr: 63,
  length: 30,
  type: 253,
  flags: 4225,
  decimals: 0,
  default: undefined,
  zeroFill: false,
  protocol41: true } ]

有时这个语句抛出:

[Object object]

我做错了什么?我无法得到正确的结果。有人可以帮助我,我遵循与其他人相同的代码,但为什么这会引发错误?

例如,尝试这个:(我在一个网站上看到这个)

CONCDB.query('SELECT * from users', function(err, rows, fields) {
  if (!err)
    console.log('The solution is: ', rows);
  else
    console.log('Error while performing Query.');
});

结果是:

The solution is:  [ RowDataPacket {
    id: 1,
    first_name: <Buffer 47 61 62 72 69 65 6c>,
    last_name: <Buffer 50 65 72 65 69 72 61> } ]

【问题讨论】:

    标签: mysql node.js


    【解决方案1】:
    console.log(results);
    

    这对我来说很好,但我想在嵌套查询中使用结果,例如:

    results[0].someAttribute
    

    适用于来自谷歌的任何人。

    【讨论】:

      【解决方案2】:

      [对象对象]

      不是错误,它只是告诉你结果的typedef

      要打印结果中包含的值,只需尝试使用:

      console.log(results)
      

      而不是

      console.log("the solution is "+results)
      

      或者如果想要深度打印

      console.log(JSON.stringify(results))
      

      【讨论】:

      • 牧马人您好,感谢您的回答。这是使用 console.log(results) 的结果: [ RowDataPacket { first_name: } ]
      • 简单名。 “解决方案是”只是完成陈述的示例。
      • @zagk 你检查过 typeof sql 表中的名字了吗??
      【解决方案3】:

      我只写了console.log(results)

      这解决了我的问题。

      【讨论】:

        猜你喜欢
        • 2015-05-15
        • 2011-07-13
        • 2019-01-13
        • 1970-01-01
        • 2021-01-29
        • 1970-01-01
        • 2019-08-12
        • 1970-01-01
        • 2022-06-12
        相关资源
        最近更新 更多