2017-07-03 62 views
0

我是超級新手postgres並試圖做一個簡單的選擇查詢。選擇查詢postgreqs在nodejs上失敗

我的表如下所示:

create table push_table 
(
    seq_id int, 
    device_id text, 
    primary key (seq_id), 
    user_id varchar(100) 
); 

和我的NodeJS代碼如下:

pool.connect(function (err, client, done) 
    { 
    if (err) console.log(err) 
    myClient = client 

    //query if the user exists 
    var ageQuery = format('SELECT * from push_table WHERE user_id = ' + userId); 
    myClient.query(ageQuery, function (err, result) { 
     if (err) { 
      console.log(err) 
     } 
     console.log("number of rows:" + result.rows.length); 
    }); 
    }); 

上運行nodejs服務器日誌打印:

{ [error: missing FROM-clause entry for table "catherine"] 
    name: 'error', 
    length: 113, 
    severity: 'ERROR', 
    code: '42P01', 
    detail: undefined, 
    hint: undefined, 
    position: '42', 
    internalPosition: undefined, 
    internalQuery: undefined, 
    where: undefined, 
    schema: undefined, 
    table: undefined, 
    column: undefined, 
    dataType: undefined, 
    constraint: undefined, 
    file: 'parse_relation.c', 
    line: '3129', 
    routine: 'errorMissingRTE' } 
/data/OSC_Push_OOW/releases/20170703215021/api/controllers/pushcontroller.js:58 
          console.log("number of rows:" + result.rows.length); 
                   ^

TypeError: Cannot read property 'rows' of undefined 
    at null.callback (/data/OSC_Push_OOW/releases/20170703215021/api/controllers/pushcontroller.js:58:46) 
    at Query.handleError (/data/OSC_Push_OOW/shared/node_modules/pg/lib/query.js:161:17) 
    at null.<anonymous> (/data/OSC_Push_OOW/shared/node_modules/pg/lib/client.js:188:26) 
    at emitOne (events.js:77:13) 
    at emit (events.js:169:7) 
    at Socket.<anonymous> (/data/OSC_Push_OOW/shared/node_modules/pg/lib/connection.js:133:12) 
    at emitOne (events.js:77:13) 
    at Socket.emit (events.js:169:7) 
    at readableAddChunk (_stream_readable.js:153:18) 
    at Socket.Readable.push (_stream_readable.js:111:10) 
+1

當發生這種情況時,你可以記錄'userId'的值嗎?可能你正在進行某種SQL注入? – joanolo

+1

以及我不得不修改並添加明確的報價,使其工作。所以你的反饋肯定幫助我 – Vik

回答

1

列user_id是VARCHAR:

user_id varchar(100) 

這樣一個有效的SQL查詢必須使用單引號來定義VARCHAR值,例如:

SELECT * from push_table WHERE user_id = 'Vik' 

您的代碼不提供所需的報價。

+0

是啊想出來,但爲其他人的好處標記爲答案 – Vik