-3

I'm making a bot with node.js & tmi.js... As well, i want to make a "command" to query a random phrase from my database & reply at chat.

I know how to do the command & the query, but I don't know how to save the "result" of the query in to "var" & use it as string.

I tried "String(Object)" but it reply me "Object, Object".

https://gyazo.com/5fd1c04c1790dc42a95e32533b27ff8b https://gyazo.com/8d4097e3818449bfb4a3c3dac1b73ef9 That is my actually code &... That... is what results https://gyazo.com/5746aca496ec8739751f6eac3682c1fc

  • 4
    Please post your code in the question itself instead of in a screenshot. – Mike G Jun 11 '19 at 13:52
  • 1
    Your connection will be an async process so you _can't_ assign its value to a variable in the traditional manner. [Look at this instead](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call). – Andy Jun 11 '19 at 13:53
  • 1
    Maybe what you are looking for is JSON.stringify(your_result_object). – Ajit Singh Jun 11 '19 at 13:55

1 Answers1

0

You can store function in var and call that query function inside it so that whenever you want it you can use it. But it needed to be handled asynchronously.

for example

var ciste = function(callback){
    connection.query("your query",function(error,result){
        callback(error,result);
    })
}

usage

ciste(function(error,result){

    //do whatever with this error and result

})

in case you need sequential process with these async methods and you know how to use callbacks, go through "npm async" or use "promise"

Kanad Chourasia
  • 501
  • 4
  • 12