NodeRed函数节点崩溃 - [红色]未捕获异常:

时间:2016-06-07 20:34:35

标签: ibm-cloud node-red

我很难理解为什么我的Node-Red Flow会因[RED] Uncaught Exception而崩溃。几乎看起来函数节点崩溃了,我在函数节点内部的代码永远都没有机会捕获它。

我有一个非常简单的Node-Red流程,它执行IBMDB node.js库以将数据插入到我的数据库中。为了使用IBMDB库,我必须将包添加到packages.json文件中的包列表中。我还必须在Bluemix-setings.js文件中设置一个全局上下文变量。我将此全局上下文变量命名为IBMDB,它等同于require语句。完成后,我可以在函数节点内使用库。

**这是我正在使用的Node.js IBMDB库。 https://github.com/ibmdb/node-ibm_db

**这是Flow Node-Red Flow

**这是函数内部的代码。

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

当我使用有效的SQL语句运行它并且所有数据类型都被正确发送时,我没有错误。一切正常!!

***问题:当我做一些强制SQL错误的测试时,Node-Red实例崩溃了。我通过发送有效的SQL语句强制错误,但其中一个字段中的数据元素是非数字值,其中表定义仅期望数字。我在错误日志(控制台输出)中看到两行..

  • 显示的错误SQL错误消息正是我期望在我的应用程序中捕获的内容。而应用程序只是崩溃。
  

[red]未捕获异常:错误:[IBM] [CLI驱动程序] [DB2 / LINUXX8664]   SQL0103N数字文字“1A”无效。 SQLSTATE = 42604

对于真正发生的事情的任何见解将不胜感激。我想知道如果我将此代码放在自定义构建的节点中,我是否能够捕获错误。这可能是使用函数节点作为我的代码的包装器的限制。 ???? *

1 个答案:

答案 0 :(得分:0)

尝试在其自己的try / catch块中包装conn.querySync调用。当前块超出范围,因为调用发生在另一个函数中(传递给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: ", err); 
}