Parametized INSERT query with node-mssql - node.js

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.
Code:
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';
}
}
});
Error:
at exports.Manager.execute (C:\Users\A\sqltest\node_modules#hapi\hapi\lib\toolkit.js:60: 33)
at Object.internals.handler (C:\Users\A\sqltest\node_modules#hapi\hapi\lib\handler.js:46 :48)
at exports.execute (C:\Users\A\sqltest\node_modules#hapi\hapi\lib\handler.js:31:36)
at Request._lifecycle (C:\Users\A\sqltest\node_modules#hapi\hapi\lib\request.js:365:68)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at async Request._execute (C:\Users\A\sqltest\node_modules#hapi\hapi\lib\request.js:274: 9)
Used node modules:
hapi/hapi 19.0.5
mssql: 6.0.1
Does anyone have an idea or or a suggestion?

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}))`
Docs:
https://www.npmjs.com/package/mssql

Related

Proper way to handle fetch errors in NodeJS v18?

I switched to NodeJS v18 with the built-in fetch and I'm using it as such:
async function get511AK() {
let res = await fetch(URL, { method: 'GET' })
if (res.ok && (res.headers.get('content-type').includes('json'))) {
let data = await res.json();
jsonresponseAK = data;
} else {
console.log("(" + res.url + ') is not json');
}
}
However, sometimes I'm getting a timeout on the URL, which is going to happen, but it's causing the script to exit. I've tried wrapping this in try/catch and it did not prevent it from exiting.
This never happened in Node v12 under the node-fetch library. What else can I add to control those connection timeouts?
node:internal/deps/undici/undici:11118
Error.captureStackTrace(err, this);
^
TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11118:11)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Timeout.get511AK [as _onTimeout] (/home/wazebot/dot-scripts/script-relay.js:76:12) {
cause: ConnectTimeoutError: Connect Timeout Error
at onConnectTimeout (node:internal/deps/undici/undici:6625:28)
at node:internal/deps/undici/undici:6583:50
at Immediate._onImmediate (node:internal/deps/undici/undici:6614:13)
at process.processImmediate (node:internal/timers:471:21) {
code: 'UND_ERR_CONNECT_TIMEOUT'
}
}
Node.js v18.12.1
Hope it helped!
process.on('uncaughtException', console.log);
// Uncaught Exception thrown - when you throw an error and did not catch anywhere.
process.on('unhandledRejection', console.log);
// Unhandled Rejection at Promise - similar, when you fail to catch a Promise.reject.

Hapi.js Cannot read property 'statusCode' of null

I'm creating a node.js api server using hapi.js and mongodb and I'm having some trouble to get it working on Amazon EC2.
Running it locally works, but if I run it on an EC2 instance I'm getting the error TypeError: Cannot read property 'statusCode' of null
The complete stacktrace is the following:
TypeError: Cannot read property 'statusCode' of null
at Request._finalize (/home/ec2-user/backend/node_modules/#hapi/hapi/lib/request.js:497:31)
at Request._reply (/home/ec2-user/backend/node_modules/#hapi/hapi/lib/request.js:434:18)
at Request._execute (/home/ec2-user/backend/node_modules/#hapi/hapi/lib/request.js:280:14)
at processTicksAndRejections (node:internal/process/task_queues:93:5)
The strange part is that GET requests are working while PUT, POST and DELETE are throwing the above error.
I've setup the server.js as follow:
...
const init = async () => {
const server = Hapi.server({
port: 3000,
});
//server.route(routes);
server.route([
{
method: "GET",
path: "/test",
handler: async (request, h) => {
return "workin GET";
},
},
{
method: "PUT",
path: "/test",
handler: async (request, h) => {
return "workin PUT";
},
},
{
method: "POST",
path: "/test",
handler: async (request, h) => {
return "workin POST";
},
},
{
method: "DELETE",
path: "/test",
handler: async (request, h) => {
return "workin DELETE";
},
},
]);
await server.start();
console.log('Server running on %s', server.info.uri);
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();
Any solution?
I've found out that on the EC2 instance I had installed node version 15.5.0 which apparently is not compatible with the latest version of hapi.js (20.0.2).
To fix the issue just install node version 14.15.3.
This is fixed in #hapi/hapi v20.2.1: https://github.com/hapijs/hapi/issues/4319.
Just remove #hapi/hapi and re-install it

Mongoose Unexpected token u in JSON at position 0

I'm creating an AWS Lambda API with Node JS. There's a template for the db connection itself, but when I run a test in Lambda I get an error and I believe the problem is the connection itself.
Here's the error message I get with Lambda test:
START RequestId: 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 Version: $LATEST
2020-05-23T01:46:23.685Z 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 INFO => using new database connection
2020-05-23T01:46:23.689Z 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 ERROR (node:8) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
2020-05-23T01:46:23.726Z 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 ERROR (node:8) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
2020-05-23T01:46:23.828Z 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"SyntaxError: Unexpected token u in JSON at position 0","reason":{"errorType":"SyntaxError","errorMessage":"Unexpected token u in JSON at position 0","stack":["SyntaxError: Unexpected token u in JSON at position 0"," at JSON.parse ()"," at /var/task/handler.js:17:22"," at processTicksAndRejections (internal/process/task_queues.js:97:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: SyntaxError: Unexpected token u in JSON at position 0"," at process. (/var/runtime/index.js:35:15)"," at process.emit (events.js:310:20)"," at processPromiseRejections (internal/process/promises.js:209:33)"," at processTicksAndRejections (internal/process/task_queues.js:98:32)"]}
END RequestId: 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5
REPORT RequestId: 6b9eef97-9c54-44bc-8ecc-dfbec7e200e5 Duration: 186.61 ms Billed Duration: 200 ms Memory Size: 1024 MB Max Memory Used: 34 MB
Unknown application error occurred
DB.js
const mongoose = require("mongoose");
mongoose.Promise = global.Promise;
let isConnected;
module.exports = connectToDatabase = () => {
if (isConnected) {
console.log("=> using existing database connection");
return Promise.resolve();
}
console.log("=> using new database connection");
return mongoose.connect(process.env.DB).then((db) => {
isConnected = db.connections[0].readyState;
});
};
Handler.js
"use strict";
const connectToDatabase = require("./db");
const Lead = require("./leads.model.js");
require("dotenv").config({ path: "./variables.env" });
module.exports.hello = (event, context, callback) => {
console.log("Hello World");
callback(null, "Hello World");
};
module.exports.create = (event, context, callback) => {
context.callbackWaitsForEmptyEventLoop = false;
connectToDatabase().then(() => {
Lead.create(JSON.parse(event.body))
.then((lead) =>
callback(null, {
statusCode: 200,
body: JSON.stringify(lead),
})
)
.catch((err) =>
callback(null, {
statusCode: err.statusCode || 500,
headers: { "Content-Type": "text/plain" },
body: "Could not create the lead.",
})
);
});
};
variables.env
DB=mongodb+srv://dbUser:password#api-jodqc.mongodb.net/test?retryWrites=true&w=majority
Whenever I see Unexpected token u in JSON at position 0, it usually means JSON.parse is trying to parse undefined. Looking at the code, I only see you executing parse in one spot
Lead.create(JSON.parse(event.body))
I'm guessing when you execute the Lambda, you're passing in an object that looks like a lead directly, so maybe something like below.
{
isALead: true
}
Instead, you need to test with an object that looks like
{
body: "{ \"isALead\": true }"
}

Prepared statements with Node-MSSQL

I want to store API data in a SQL Server database with Node.js. It works already with a normal INSERT query as you see in the first code below.
server.route({
method: 'POST',
path: '/',
handler: async(request, h) => {
try {
await pool.query("INSERT INTO mytable(device,data,stationId,rssi,unix_timestamp) VALUES('"+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';
}
}
});
Now my I want to improve the code a little bit to avoid SQL injection and to have a better overview.
Therefore I want to use prepared statements like described in this documentation: https://www.npmjs.com/package/mssql#prepared-statement
So far I've managed to do this here:
server.route({
method: 'POST',
path: '/',
handler: async(request, h) => {
const ps = new sql.PreparedStatement(pool)
try {
ps.input('device', sql.VarChar(10))
ps.input('data', sql.VarChar(24))
ps.input('station', sql.NChar(10))
ps.input('rssi', sql.Float)
ps.input('time', sql.Int)
await ps.prepare('INSERT INTO mytable(device,data,stationId,rssi,unix_timestamp) VALUES(#device,#data,#station,#rssi,#time)');
try {
await ps.execute(
{ device: request.payload.device },
{ data: request.payload.data },
{ station: request.payload.station },
{ rssi: request.payload.rssi },
{ time: request.payload.time }
)
} finally {
await ps.unprepare();
}
return h.response('Callback received').code(200);
}
catch (err) {
console.log("SQL Err", err.stack);
return 'Error';
}
}
});
And the following error occurs:
at Parser.emit (events.js:223:5)
at Parser.<anonymous> (C:\Users\AW\sqltest\node_modules\tedious\lib\token\token-stream-parser.js:37:14)
at Parser.emit (events.js:223:5)
at addChunk (C:\Users\AW\sqltest\node_modules\readable-stream\lib\_stream_readable.js:297:12)
at readableAddChunk (C:\Users\AW\sqltest\node_modules\readable-stream\lib\_stream_readable.js:279:11)
at Parser.Readable.push (C:\Users\AW\sqltest\node_modules\readable-stream\lib\_stream_readable.js:240:10)
at Parser.Transform.push (C:\Users\AW\sqltest\node_modules\readable-stream\lib\_stream_transform.js:139:32)
This is an example of the JSON data I get from the API:
{
"device":"887B53",
"data":"4660000000000062b4a8",
"station":"1B2C"
"rssi":"-123",
"time":"1585258718"
}
I hope someone can help me with that.
It appears that the error is occurring at the following line:
ps.input('station', sql.NChar(10))
I would debug the value that is being assigned here and see if it fits the schema, or if it's undefined.
Additionally on a side note if you are worried about readability and SQL injections, consider using an ORM such as Sequelize.

How to test a function that throws an error asynchronously, using tape?

I am attempting to test this module (receiver.js) for an error thrown:
var request = require('request')
module.exports = function(url){
request({
url: url,
method: 'POST'
}, function(error) {
if(error){
throw error
}
})
}
using this test (test.js):
var test = require('tape')
test('Receiver test', function(t){
var receiver = require('./receiver')
t.throws(function(){
receiver('http://localhost:9999') // dummy url
}, Error, 'Should throw error with invalid URL')
t.end()
})
but tape runs the assertion before the error is thrown, resulting in the following error message:
TAP version 13
# Receiver test
not ok 1 Should throw error with invalid URL
---
operator: throws
expected: |-
[Function: Error]
actual: |-
undefined
at: Test.<anonymous> (/path/to/tape-async-error-test/test.js:5:4)
...
/path/to/receiver.js:9
throw error
^
Error: connect ECONNREFUSED 127.0.0.1:9999
at Object.exports._errnoException (util.js:856:11)
at exports._exceptionWithHostPort (util.js:879:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1062:14)
Is there a way around this?
Generally, using tape, you have to ensure you call assert.end() after the async call has completed. Using promises (would require request-promise and returning the promise):
test('Receiver test', function(t){
// Tells tape to expec a single assertion
t.plan(1);
receiver('http://localhost:9999')
.then(() => {
t.fail('request should not succeed')
})
.catch(err => {
t.ok(err, 'Got expected error');
})
.finally({
t.end();
});
});
Using async/await:
test('Receiver test', async function(t) {
try {
await receiver('http://localhost:9999');
assert.fail('Should not get here');
} catch (err) {
assert.ok(err, 'Got expected error');
}
t.end();
});
The above example is mostly correct but here's a complete working example that compares async to synchronous side by side and also shows how to check for the error message in a manner similar to the tape examples given on tape's README.md.
test('ensure async function can be tested to throw', async function(t) {
// t.throw works synchronously
function normalThrower() {
throw(new Error('an artificial synchronous error'));
};
t.throws(function () { normalThrower() }, /artificial/, 'should be able to test that a normal function throws an artificial error');
// you have to do this for async functions, you can't just insert async into t.throws
async function asyncThrower() {
throw(new Error('an artificial asynchronous error'));
};
try {
await asyncThrower();
t.fail('async thrower did not throw');
} catch (e) {
t.match(e.message,/asynchronous/, 'asynchronous error was thrown');
};
});

Resources