inside a multidimensional array, created by a mysql query, I have saved the connection parameters to multiple opcua machines. I believe the problem is the async functions inside the loop, but I don't know how to fix it.
When I run the script, no sessions are started and this error is returned
myprj/node-opcua-assert/dist/index.js:11
const err = new Error(message);
^
Error
at assert (myprj/node-opcua-assert/dist/index.js:11:21)
at OPCUAClientImpl._createSession (myprj/node-opcua-client/dist/private/opcua_client_impl.js:856:40)
at OPCUAClientImpl.createSession (myprj/node-opcua-client/dist/private/opcua_client_impl.js:273:14)
at OPCUAClientImpl.createSession (myprj/thenify/index.js:65:46)
at OPCUAClientImpl.createSession (myprj/node-opcua-client/dist/private/opcua_client_impl.js:267:25)
at myprj/thenify/index.js:72:10
at new Promise (<anonymous>)
at OPCUAClientImpl.createSession (myprj/thenify/index.js:70:12)
at myprj/read_node.js:8
The code is:
/*
Here is the code not listed for import settings and settings connection database
*/
const endpointUrl = config.endpointUrl;
const opcua = require("node-opcua");
const ping = require('ping');
const {
AttributeIds,
OPCUAClient
} = require("node-opcua");
const chalk = require("chalk");
const mysql = require('mysql2');
/*receive arguments*/
var argvs = process.argv.slice(2);
var args = [];
argvs.forEach(function (val, index, array) {
var arg = val.split('=');
args[arg[0]] = arg[1];
});
/*
Db connection
*/
var con = mysql.createConnection(config.mysql);
con.connect(function(err) {
if (err) throw err;
console.log(chalk.green("DB Connected!"));
});
/*
Reading machine->recipes->nodes
*/
;(async () => {
try {
var sql = "";
sql = "SELECT * FROM machine ...";
con.query(sql, function(err, machines) {
if (err) throw err;
/*
Machine list
*/
var m = 0;
machines.forEach(async machine => {
/*
Check if machine is alive
*/
let HostInfo = await ping.promise.probe(machine.ip);
console.log(HostInfo.alive);
if(HostInfo.alive)
{
console.log('Host ' + machine.ip + ' raggiungibile!');
var endpointUrl = machine.ip + ':' + machine.port;
console.log(endpointUrl);
client = OPCUAClient.create({
endpointMustExist: false,
});
console.log(" connecting to ", chalk.cyan(endpointUrl));
await client.connect(endpointUrl);
console.log(" connected to ", chalk.green(endpointUrl));
session = await client.createSession();
console.log(" session created".yellow);
var sql = "";
sql = "SELECT * FROM machine_recipes...";
/*
Machine recipes
*/
con.query(sql, function(err, machine_recipes) {
if (err) throw err;
machine_recipes.forEach(async machine_recipe => {
/*
Machine recipes NODES to check
*/
var sql = "";
sql = "SELECT * FROM machine_recipes_nodes...";
con.query(sql, function(err, nodes) {
var i = 0;
nodes.forEach(async node => {
var params = JSON.parse(node.params);
var dataValues = await session.read([{
nodeId: params.unitid
}]);
var node = params.unitid;
var value = dataValues [0].value.value;
/*
Saving value in database...
*/
});
});
});
});
}
m++;
});
});
} catch (err) {
console.log('error !', err.message);
}
})();
Thanks for help