0

I'm currently running SailsJS on a Raspberry Pi and all is working well however when I execute a sails.models.nameofmodel.count() when I attempt to respond with the result I end up getting a empty response.

getListCount: function(req,res)
    {
            var mainsource = req.param("source");

            if(mainsource)
            {
                sails.models.gatherer.find({source: mainsource}).exec(
                        function(error, found)
                        {
                                if(error)
                                {
                                        return res.serverError("Error in call");
                                }
                                else
                                {
                                        sails.log("Number found "+found.length);
                                        return res.ok({count: found.length});
                                }
                        }   
                );
        }
        else
        {
                return res.ok("Error in parameter");
        }   
},

I am able to see in the logs the number that was found (73689). However when responding I still get an empty response. I am using the default stock ok.js file, however I did stick in additional logging to try to debug and make sure it is going through the correct paths. I was able to confirm that the ok.js was going through this path

if (req.wantsJSON) {
    return res.jsonx(data);
}

I also tried adding .populate() to the call before the .exec(), res.status(200) before I sent out a res.send() instead of res.ok(). I've also updated Sails to 11.5 and still getting the same empty response. I've also used a sails.models.gatherer.count() call with the same result.

  • Shouldn't you be including a `return` before each `res.ok()` call as mentioned in the documentation? Same for `res.serverError()`. See here: http://sailsjs.org/documentation/reference/response-res/res-ok – Rai Feb 08 '16 at 03:37
  • I did add that to the callback but still am getting an empty response error. When you use the res.ok() or res.serverError() it goes into the responses and in the responses it uses the return res.jsonx(data). – macleod2486 Feb 08 '16 at 04:06
  • If you have added the `return` could you please add it to your post above? Also, please show how you are calling `getListCount`. – Rai Feb 08 '16 at 11:03
  • I am putting this in the routes.js '/getListCount':{ controller: 'GathererController', action: 'getListCount' }, – macleod2486 Feb 09 '16 at 07:33
  • Please see my answer below. – Rai Feb 09 '16 at 12:30

1 Answers1

0

You can try to add some logging to the beginning of your method to capture the value of mainsource. I do not believe you need to use an explicit return for any response object calls.

If all looks normal there, try to eliminate the model's find method and just evaluate the request parameter and return a simple response:

getListCount: function(req, res) {
    var mainsource = req.param("source");
    sails.log("Value of mainsource:" + mainsource);

    if (mainsource) {
      res.send("Hello!");
    } else {
      res.badRequest("Sorry, missing source."); 
    }
}

If that does not work, then your model data may not actually be matching on the criteria that you are providing and the problem may lie there; in which case, your response would be null. You mentioned that you do see the resulting count of the query within the log statement. If the res.badRequest is also null, then you may have a problem with the version of express that is installed within sailsjs. You mention that you have 11.5 of sailsjs. I will assume you mean 0.11.5.

This is what is found in package.json of 0.11.5

   "express": "^3.21.0",

Check for any possible bugs within the GitHub issues for sailsjs regarding express and response object handling and the above version of express.

It may be worthwhile to perform a clean install using the latest sailsjs version (0.12.0) and see if that fixes your issue.

Another issue may be in how you are handling the response. In this case .exec should execute the query immediately (i.e. a synchronous call) and return the response when complete. So there should be no asynchronous processing there.

If you can show the code that is consuming the response, that would be helpful. I am assuming that there is a view that is showing the response via AJAX or some kind of form POST that is being performed. If that is where you are seeing the null response, then perhaps the problem lies in the view layer rather than the controller/model.

If you are experiencing a true timeout error via HTTP even though your query returns with a result just in time, then you may need to consider using async processing with sailjs. Take a look at this post on using a Promise instead.

Community
  • 1
  • 1
Rai
  • 394
  • 5
  • 23