簡體   English   中英

使用 node-mssql 的參數化 INSERT 查詢

[英]Parametized INSERT query with node-mssql

我想使用 node.js 為 SQL 服務器參數化插入查詢。 不幸的是,它不起作用,我真的不知道這是節點模塊問題還是語法錯誤。

代碼:

server.route({
    method: 'POST',
    path: '/',
    handler: async (request, h) => {

    try {
        await pool.query("INSERT INTO sigfoxmessages(device,data,station,rssi,unix_timestamp) VALUES($1,$2,$3,$4,$5))"
        [request.payload.device, request.payload.data, request.payload.station, request.payload.rssi, request.payload.time]);

        return h.response('Callback received').code(200);
    }
    catch (err) {
        console.log("SQL Err", err.stack);
        return 'Error';
    }
  }
});

錯誤:

在exports.Manager.execute (C:\Users\A\sqltest\node_modules@hapi\hapi\lib\toolkit.js:60:33)
在 Object.internals.handler (C:\Users\A\sqltest\node_modules@hapi\hapi\lib\handler.js:46:48)
在exports.execute (C:\Users\A\sqltest\node_modules@hapi\hapi\lib\handler.js:31:36)
在 Request._lifecycle (C:\Users\A\sqltest\node_modules@hapi\hapi\lib\request.js:365:68)
在 processTicksAndRejections (internal/process/task_queues.js:94:5)
在異步 Request._execute (C:\Users\A\sqltest\node_modules@hapi\hapi\lib\request.js:274:9)

使用的節點模塊:

  • hapi/hapi 19.0.5
  • mssql:6.0.1

有沒有人有想法或建議?

根據mssql的文檔,您可以在 INSERT 語句中使用 es6 模板文字。

pool.query`INSERT INTO sigfoxmessages (device,data,station,rssi,unix_timestamp) VALUES(${request.payload.device}, ${request.payload.data}, ${request.payload.station}, ${request.payload.rssi}, ${request.payload.time}))`

文檔: https://www.npmjs.com/package/mssql

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM