watsonx Assistant

watsonx Assistant

Conversational AI for fast and friendly customer care

 View Only
  • 1.  Funcion (with NodeJs) to access MySql DB

    Posted Fri September 18, 2020 12:42 PM
    I am facing several difficulties to developer one Watson Function to access my MySql DB.
    The code below is what I had tried so far :

    /**
    /**
    *
    * main() will be run when you invoke this action
    *
    * @param Cloud Functions actions accept a single parameter, which must be a JSON object.
    *
    * @return The output of this action, which must be a JSON object.
    *
    */
    function main(params) {
    //Conexao com BD MySQL
    const mysql = require('mysql');

    //A partir do MySQL 8 apresenta o erro ao utilizar o usuário root para conexão, necessário criar novo usuário (instrução no Readme)
    const connection = mysql.createConnection({
    host: 'xxxxxxxxxxxxxxxxxxxxt',
    user: 'yyyyyyyyyyyyy',
    password: 'sssssssssssssssss',
    database: 'zzzzzzzzzzzzzzzz'
    });

    connection.connect(function (err) {
    if (err) {
    console.log("deu erro");
    console.log('error connecting: ' + err.stack);
    return;
    }
    console.log('*****OK*****');
    console.log ('connected as id : ' + connection.threadId);
    });


    console.log('** FINAL **');
    return { message: 'Ropando CF com sucesso - MSG : ' + params.user_input };
    }

    With this code I am getting this error :

    Resultados:
    {
      "error": "The action did not produce a valid response and exited unexpectedly."
    }
    Logs:
    [
      "2020-09-17T11:42:16.628518Z    stdout: *****OK*****",
      "2020-09-17T11:42:16.628538Z    stdout: connected as id : 242158410",
      "2020-09-17T11:43:02.895087Z    stderr: events.js:174",
      "2020-09-17T11:43:02.895115Z    stderr: throw er; // Unhandled 'error' event",
      "2020-09-17T11:43:02.895122Z    stderr: ^",
      "2020-09-17T11:43:02.895127Z    stderr: ",
      "2020-09-17T11:43:02.895132Z    stderr: Error: Connection lost: The server closed the connection.",
      "2020-09-17T11:43:02.895138Z    stderr: at Protocol.end (/node_modules/mysql/lib/protocol/Protocol.js:112:13)",
      "2020-09-17T11:43:02.895143Z    stderr: at Socket.<anonymous> (/node_modules/mysql/lib/Connection.js:97:28)",
      "2020-09-17T11:43:02.895148Z    stderr: at Socket.<anonymous> (/node_modules/mysql/lib/Connection.js:502:10)",
      "2020-09-17T11:43:02.895153Z    stderr: at Socket.emit (events.js:203:15)",
      "2020-09-17T11:43:02.895159Z    stderr: at endReadableNT (_stream_readable.js:1145:12)",
      "2020-09-17T11:43:02.895164Z    stderr: at process._tickCallback (internal/process/next_tick.js:63:19)",
      "2020-09-17T11:43:02.895169Z    stderr: Emitted 'error' event at:",
      "2020-09-17T11:43:02.895174Z    stderr: at Connection._handleProtocolError (/node_modules/mysql/lib/Connection.js:425:8)",
      "2020-09-17T11:43:02.895179Z    stderr: at Protocol.emit (events.js:198:13)",
      "2020-09-17T11:43:02.895184Z    stderr: at Protocol._delegateError (/node_modules/mysql/lib/protocol/Protocol.js:390:10)",
      "2020-09-17T11:43:02.895190Z    stderr: at Protocol.end (/node_modules/mysql/lib/protocol/Protocol.js:116:8)",
      "2020-09-17T11:4


    Can someone tell me what is wrong with it..?
    Or someone has an example of how to do it .?

    Thank you in advance.

    ------------------------------
    Eduardo Gameiro
    ------------------------------

    #WatsonAssistant


  • 2.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Mon September 21, 2020 10:34 AM
    which version of Node.js are you using? you may need to upgrade the function to version 10

    ------------------------------
    CHRIS CRANE
    ------------------------------



  • 3.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Tue September 22, 2020 06:30 AM
    Hello Chis...

    Thank you for your replay.

    I am using the version 10 of Node.js

    Regards.

    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 4.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Mon September 21, 2020 10:34 AM
    Ola Eduardo!

    Not sure why you are not successfully connecting, but if you want a good example of how to connect to MySQL with some Node code, then the Getting Started example from the MySQL documentation might be a good place to start (https://cloud.ibm.com/docs/ComposeForMySQL?topic=ComposeForMySQL-getting-started).  The Github project with the code is here (https://github.com/IBM-Cloud/compose-mysql-helloworld-nodejs).

    ------------------------------
    Daniel Toczala
    Community Leader and Customer Success Manager - Watson
    dtoczala@us.ibm.com
    ------------------------------



  • 5.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Tue September 22, 2020 06:33 AM
    Hello Daniel...

    Thank you for your replay.

    I will take a look on it.

    Regards.

    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 6.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Mon September 21, 2020 12:32 PM
    Hi Eduardo!

    Looking at your logs, I can see that you are connecting with the thread ID 242158410, but it is disconnecting about 45 seconds later. It may be timing out due to inactivity. I found a similar issue that another Node MySQL user was having here: https://github.com/sidorares/node-mysql2/issues/836#issuecomment-414281593 Try using mysql.createPool instead of mysql.createConnection for your connection. Something else to check may be your timeout settings in MySQL. Let us know if this helps!

    ------------------------------
    Morgan Langlais
    ------------------------------



  • 7.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Tue September 22, 2020 06:37 AM
    Hy Morgan.

    I applied your suggestion and now I am trying to implement de Declan suggestion in order to see if I get success in the result.

    Regards.


    ------------------------------
    Eduardo Gameiro
    ------------------------------



  • 8.  RE: Funcion (with NodeJs) to access MySql DB

    Posted Tue September 22, 2020 04:21 AM
    Hi Eduardo,
    I'm not sure if it's your problem, but I always use a Promise when calling APIs/DBs in Cloud Functions.
    Nodejs code often returns before the connection has ended and can cause errors like you're seeing.
    So if you wrap your DB call inside a promise it might help.
    Here's one I did for an API:

    return new Promise(function (resolve, reject) {
    request(options,propertiesObject, function (err, resp) {
    if (err) {
    console.log(err);
    return reject({err: err});
    }
    var decoded = JSON.parse(resp.body);

    return resolve({decoded});
    });
    });
    Regards,
    Declan

    #WatsonAssistant