0

app.js

app.get('/api/search/:hashtag/:sinceid/:resultType', function(req, res)
{
  //var obj = searchdb.search('rooney',connection);

  obj = searchdb.search('rooney',connection);

  console.log("obj is" + obj);
}

I have this part of code in app.js calling search function in another js file to search for a query but it always come undefined. Although the obj is being shown as [object Object] printed in console.log statement of 2nd part of code. Am I returning "statuses" correctly?

  var alerts = [];
  var text;
  var jsonString;
  module.exports = {search: function mydb(query_search,connection){

  query_search = "%#" + query_search + "%";
     connection.query('select * from `team079`.`tweets` where text like ?',query_search, function(err,res){
       if(err)
       {
         throw err;
         console.log(err);
       }
       else{
        for (var i in res){
          var tweetText = res[i].text;
          var screenName = res[i].screen_name;
          var created_at = res[i].created_at;

          text = {text: tweetText, screen_name: screenName,created_at: created_at };

          alerts.push(text);
        console.log("Tweet:" + tweetText + "\n\n" + screenName + "\n\n" + created_at + "\n\n    ");
      }

    var statuses = {"statuses":alerts};
    console.log("searchdb obj is " + statuses);
    var string = JSON.stringify(statuses);
    jsonString = JSON.parse(string);
    return statuses;
      }
   })
   }
Priya
  • 1,359
  • 6
  • 21
  • 41
  • Don't use `+` to concatenate the object to the string, use comma (`,`) instead: `console.log("obj is", obj);` – JJJ Apr 18 '16 at 07:42
  • Juhana that didn't make a difference. Actually I think my console.log in app.js is executed before searchdb returns "statuses". How do I prevent that? – Fahad Zulfiqar Apr 18 '16 at 07:48
  • Yes, that is what happens. Read the duplicate. – JJJ Apr 18 '16 at 07:49

0 Answers0