I am having a hard time understanding why my Node-Red Flow keeps crashing with an [RED] Uncaught Exception. It almost seems like the function node is crashing and my code inside of the function node never has a chance to catch it.
I have a very simple Node-Red flow that executes a IBMDB node.js library to insert data into my database. In order to use the IBMDB library, I had to add the package to the list of packages in my packages.json file. I also had to setup a global context variable inside the Bluemix-setings.js file. I named this global context variable IBMDB, which is the equivalent to the require statement. Once that was done, I am able to make use of the library inside a function node.
** Here is the Node.js IBMDB library, I am using.
https://github.com/ibmdb/node-ibm_db
** Here is the Flow
** Here is the code inside the function.
try {
context.global.ibmdb.open("DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;", function (err,conn) {
conn.beginTransaction(function (err) {
if (err) {
//could not begin a transaction for some reason.
console.log(err);
return conn.closeSync();
}
for (i = 0; i < msg.payload.Readings.length; i++)
{
var result = conn.querySync("INSERT INTO db2.table (" +
"I_ID," +
"D_ID," +
"field1," +
"field2," +
"field3," +
"field4," +
"field5," +
"field6," +
"field7," +
"field8," +
"field9," +
"field10)" +
"values (" +
"1A," +
"'"+ msg.payload.field1 + "',"+
"'" + JSON.stringify(msg.payload.Readings[i].field2) + "'," +
msg.payload.Readings[i].field3 + ","+
msg.payload.Readings[i].field4 + ","+
msg.payload.Readings[i].field5 + ","+
msg.payload.Readings[i].field6 + ","+
msg.payload.Readings[i].field7 + ","+
msg.payload.Readings[i].field8 + ","+
msg.payload.Readings[i].field9 + ","+
msg.payload.Readings[i].field10 + ","+
"'2016-05-31 22:28:51.000000'" +
");");
}
conn.commitTransaction(function (err) {
if (err) {
//error during commit
console.log("****ERROR: " + err);
node.error("**** ERROR: ", err);
//return conn.closeSync();
}
//Close the connection
conn.closeSync();
});
});
});
} catch (e) {
node.error("**** ERROR: ", err);
}
When I run this with a valid SQL statement and all the data types are correctly sent, I get no errors. Everything works!!
*** The issue: When I did some testing to force a SQL error, the Node-Red instance crashes. I forced the error by sending a valid SQL statement, but the data elements in one of the fields is a non-numeric value where the table definition is expecting numeric only. I see in the error logs (console out) two lines..
The error SQL error message displayed is exactly what I was expecting to catch in my application. Instead the application just crashes.
[red] Uncaught Exception: Error: [IBM][CLI Driver][DB2/LINUXX8664]
SQL0103N The numeric literal "1A" is not valid. SQLSTATE=42604
Any insight on what is really happening would be greatly appreciated. I am wondering if I put this code inside a custom built node would I be able to catch the error. Could this be a limitation of using the function node as a wrapper to my code. ????
*
Try wrapping the conn.querySync call in it's own try/catch block. The current block is out of scope because the call happens in another function (the one passed into beginTransaction).
e.g.
try {
context.global.ibmdb.open("DATABASE=database;HOSTNAME=hostname;PORT=port;PROTOCOL=TCPIP;UID=username;PWD=password;", function (err,conn) {
conn.beginTransaction(function (err) {
if (err) {
//could not begin a transaction for some reason.
console.log(err);
return conn.closeSync();
}
for (i = 0; i < msg.payload.Readings.length; i++)
{
try {
var result = conn.querySync("INSERT INTO db2.table (" +
"I_ID," +
"D_ID," +
"field1," +
"field2," +
"field3," +
"field4," +
"field5," +
"field6," +
"field7," +
"field8," +
"field9," +
"field10)" +
"values (" +
"1A," +
"'"+ msg.payload.field1 + "',"+
"'" + JSON.stringify(msg.payload.Readings[i].field2) + "'," +
msg.payload.Readings[i].field3 + ","+
msg.payload.Readings[i].field4 + ","+
msg.payload.Readings[i].field5 + ","+
msg.payload.Readings[i].field6 + ","+
msg.payload.Readings[i].field7 + ","+
msg.payload.Readings[i].field8 + ","+
msg.payload.Readings[i].field9 + ","+
msg.payload.Readings[i].field10 + ","+
"'2016-05-31 22:28:51.000000'" +
");");
} catch (excp) {
//do stuff
}
}
conn.commitTransaction(function (err) {
if (err) {
//error during commit
console.log("****ERROR: " + err);
node.error("**** ERROR: ", err);
//return conn.closeSync();
}
//Close the connection
conn.closeSync();
});
});
});
} catch (e) {
node.error("**** ERROR: ", e);
}
Related
https//127.0.0.1:111001 unexpectedly closed the connection
I need to hit https//127.0.0.1:111001 for Mantra Rd service connection . but not able to hit Getting error connection closed (I am using IONIC for mobile application) The same thing is working on web with angular8.
please help...
const xhr = new XMLHttpRequest();
let url = "https://127.0.0.1:11100"
xhr.open('DEVICEINFO', url);
const params = '<?xml version="1.0"?> <PidOptions ver="1.0"> <Opts fCount="' + 5 + '" fType="' + "FMR" + '" iCount="' + 1 + '" pCount="' + 1 + '" format="' + 0 + '" pidVer="' + 2.0 + '" timeout="' + 60000 + '" posh="UNKNOWN" env="' + 'P' + '" /> ' + 'this.DemoFinalString' + '<CustOpts><Param name="mantrakey" value="' + '' + '" /></CustOpts> </PidOptions>';
// set `Content-Type` header
xhr.setRequestHeader('Content-Type', 'text/xml');
xhr.send(params);
xhr.onload = async () => {
const parser = new DOMParser();
const xml = parser.parseFromString(xhr.responseText, 'text/xml');
this.obj = await this.xmlToJsonfn(xml);
this.currentdeviceInfoSubject.next(this.obj);
};
xhr.onerror = function (e) {
console.log('eoor', e);
if (e.type == 'error') {
// alert('please download the Driver');
}
};
I am trying to display a map in my UI5 app using a formatter.js where I need to put the address with the map URL together.
In the old world the code should look like the following:
formatMapUrl: function(sStreet, sZIP, sCity, sCountry) {
return "https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=640x640&markers="
+ jQuery.sap.encodeURL(sStreet + ", " + sZIP + " " + sCity + ", " + sCountry);
},
How should I replace the deprecated function? Where should I add the new code?
If you look at the API documentation for jQuery.sap.encodeURL, you see it states to use the module sap/base/security/encodeURL now.
Usage example from documentation:
sap.ui.require(["sap/base/security/encodeURL"], function(encodeURL) {
var sEncoded = encodeURL("a/b?c=d&e");
console.log(sEncoded); // a%2fb%3fc%3dd%26e
});
Usage in the formatter.js:
sap.ui.define([
"sap/base/security/encodeURL"
], function (encodeURL) {
"use strict";
return {
formatMapUrl: function(sStreet, sZIP, sCity, sCountry) {
var sBaseUrl = "https://maps.googleapis.com/maps/api/staticmap?zoom=13&size=640x640&markers=";
var sEncodedString = encodeURL(sStreet + ", " + sZIP + " " + sCity + ", " + sCountry);
return sBaseUrl + sEncodedString;
}
};
});
I 'm working on an app in ionic framework in which I want to get location of device.
It works fine for getCurrentPosition
But when I try to use watchPosition, so that I can get new position whenever there is a change in location of device. it does nothing.
I 'm not sure what I 'm missing
my controllers.js code
.controller('LocCtrl', function($scope,$cordovaGeolocation) {
var posOptions = {timeout: 10000, enableHighAccuracy: false};
$cordovaGeolocation
.getCurrentPosition(posOptions)
.then(function (position) {
alert('Latitude: ' + position.coords.latitude + '\n' +
'Longitude: ' + position.coords.longitude + '\n' +
'Altitude: ' + position.coords.altitude + '\n' +
'Accuracy: ' + position.coords.accuracy + '\n' +
'Altitude Accuracy: ' + position.coords.altitudeAccuracy + '\n' +
'Heading: ' + position.coords.heading + '\n' +
'Speed: ' + position.coords.speed + '\n' +
'Timestamp: ' + position.timestamp + '\n');
}, function(err) {
alert(err);
});
var watchOptions = {
timeout : 3000,
enableHighAccuracy: false // may cause errors if true
};
var watch = $cordovaGeolocation.watchPosition(watchOptions);
watch.promise.then(
null,
function(err) {
alert("error:"+err);
},
function(position) {
var lat = position.coords.latitude
var long = position.coords.longitude
alert("Lat in Watch:"+lat);
});
$cordovaGeolocation.clearWatch(watch.watchId);
})
I used some alert messages to see whats happening,
In current setuation, it gives two alert messages giving getCurrentPosition, but noting happens on changing device Location.
I've got a spec where I'm grabbing a screenshot that also requires a dynamic path be created. makeDir does not return a promise, so it fires at the same time (after) saveScreenshot. What's the best way to ensure makeDir is called first?
getPath().then(function(path) {
makeDir(baseUrl + '/' + path);
saveScreenshot(baseUrl + '/' + path + '/' + filename);
})
You should be able to just chain another then() and do the second operation in there.
getPath().then(function(path) {
makeDir(baseUrl + '/' + path);
var screenshotpath = baseUrl + '/' + path + '/' + filename;
return screenshotpath;
}).then(function(screenshotpath) {
saveScreenshot(screenshotpath);
});
I am working on Phonegap application and trying to get the contacts of the device.
What I have done from this reference is as follows :
function onDeviceReady() {
var options = new ContactFindOptions();
options.filter=""; // empty search string returns all contacts
options.multiple=true; // return multiple results
filter = ["displayName"];
navigator.contacts.find(filter, onSuccess, onError, options);
}
// contacts Methods
function onSuccess(contacts) {
for (var i=0; i<contacts.length; i++) {
for (var j=0; j<contacts[i].addresses.length; j++) {
alert("Pref: " + contacts[i].addresses[j].pref + "\n" +
"Type: " + contacts[i].addresses[j].type + "\n" +
"Formatted: " + contacts[i].addresses[j].formatted + "\n" +
"Street Address: " + contacts[i].addresses[j].streetAddress + "\n" +
"Locality: " + contacts[i].addresses[j].locality + "\n" +
"Region: " + contacts[i].addresses[j].region + "\n" +
"Postal Code: " + contacts[i].addresses[j].postalCode + "\n" +
"Country: " + contacts[i].addresses[j].country);
}
}
};
// onError: Failed to get the contacts
function onError(contactError){
alert('onError!');
}
In console while printing the contacts I am getting this error-
[INFO] Error in success callback: Contacts1 = TypeError: 'null' is not an object
There are contacts in my simulator and contact information is not null still I am getting null. Not sure about the reason.
Please help me.
EDIT : same thing is happening when I am playing audio files ..
Any one have idea why this is happening ??