本文介绍了使用node-mssql进行参数化的INSERT查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用node.js为SQL Server设置插入查询的参数.不幸的是,它不起作用,我真的不知道这是节点模块问题还是语法错误.

I want to parametrize an insert query with node.js for SQL Server. Unfortunately it will not work and I don't really know if it's a Node module issue or a syntax failure.

代码:

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';
    }
  }
});

错误:

使用的节点模块:

  • hapi/hapi 19.0.5
  • mssql:6.0.1

有人有想法或建议吗?

Does anyone have an idea or or a suggestion?

推荐答案

根据mssql的文档,您可以在INSERT语句中使用es6模板文字.

According to the documentation for mssql you can use es6 template literals in you INSERT statement.

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

这篇关于使用node-mssql进行参数化的INSERT查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 08:23