2017-08-02 65 views
0

這是我簡單的聲明:MySQL查詢返回NodeJS中的[Object,object],爲什麼?

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) 

我得到與使用行。

使用功能(ERR,行,場)和返回領域我得到以下幾點:

[ 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> } ] 

回答

2

[Object object]是不是一個錯誤,它只是告訴你的類型定義結果。

要打印一個包含在結果值只是儘量只使用:

console.log(results);

,而不是

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

+0

你好牧師,謝謝你的回答。這是使用console.log(結果)的結果:[RowDataPacket {first_name:}] – zagk

+0

@zagk你想輸出什麼解釋正確...... – wrangler

+0

簡單的名字。 「解決方案」就是完成聲明的例子。 – zagk

-1

我只寫了console.log(results)

這解決了我的問題。

相關問題