title: How to concatenate array objects and strings to get a string in Javascript?
body: <p>The following code is supposed to take a string as an input(inMessage here) and split the words in it. It then queries firebase realtime database for an associated key, value and replaces the word with the value retrieved. This modified string has to be sent back as output. </p>
  
  <p>Now, I can't seem to find a way to make the 'msg' a proper string. If I hardcode a string instead of msg I get the output rendered correctly. So how do I make msg a proper string? (I've tried enclosing it with "", toString(), String() and JSON.stringify() - There has to be something I'm missing here)</p>
  
  <pre><code>  function queryDB(senderID, inMessage){
      var arr=inMessage.split(" ");
      console.log(arr);
      var i;
      console.log('inside queryDB');
      var msg="";
      for(i=0;i&lt;arr.length;i++){
      var x = 'https://oreo-fd681.firebaseio.com/'+arr[i]+'.json';
      request({
          url: x,
          method: 'GET'
        }, function(error, response, body) {
              console.log(response.body);
              if (error) {
                console.log('Error making api call ' + error);
              } else if (response.body.error){
                console.log('Error making api call' + response.body.error);
              }
              else if(response==null)  {
                //if not found in DB concatenate whatever arr[i] holds
                callback1();
              }
              else  {
                //else concatenate the found key
                var n=JSON.parse(response.body);
                //remove the quotes associated with key value
                callback2(JSON.stringify(n.key).replace(/['"]+/g, ''));
              }
        }); 
        function callback1(){
          msg+=(arr[i]);
          msg+=" ";
          console.log(msg);
        }
        function callback2(add){
          msg+=(add);
          msg+=" ";
          console.log(msg);
        }
      }
        //add quotes back - not sure of this
        sendMessageToUser(senderID, ("\""+msg+"\""));  
    }
  </code></pre>