0

I have an AngularJS service called sensorReadingsAPI, which has the following function with get:

var _countSensorReadingsFilter = function () {
  return $http.get(config.baseUrl + "/sensorReading/countFilter").then(function (data,status) {
    sensorReadingCountFilter = data.data;
    return Number(sensorReadingCountFilter);
  }).catch(function() {});
};

The /countFilter is defined in a NodeJS controller:

module.exports = {
  countFilter: function (req, res) {
    return res.sensorReadingCountFilter();
  }
};

And sensorReadingCountFilter is as follows:

module.exports = function sensorReadingCountFilter(statusCode){

  var req = this.req;
  var res = this.res;

  SensorReading.count({deviceNum:'MBA002'}).exec(function countCB(error, found) {
    return res.view({val: found});
  });
};

Right now, the count is static (where is written {deviceNum:'MBA002'}). I need a way to make this value be dinamic, using a variable, preferably being send from the AngularJS controller. I have several devices, with the portential to add even more, and don't want to create a /countFilter module for each of them.

In other words, a way to send variables from an AngularJS service to a NodeJS controller. When searching for ways to do this, I have found next to nothing.

For clearance: all of this returns the number of readings from the selected device, and config.baseUrlis the local machine and port.

1 Answer 1

1

You can use url parameters in your $http.get('url-here?param1=1,param2=2') and then you can access these params in your req object on the server side req.query.param1.

You also can specify params in config object of $http:

$http.get('my-url-here', { params: {deviceNum:'MBA002'} })
    .success()
    ...
Sign up to request clarification or add additional context in comments.

4 Comments

I wouldn't say "also". Using params is the right way to do it. Composing an URL manually usually ends up with missing encodeURIComponent calls.
I was unaware that it was possible to use url parameters using ?param. This is exactly what I need.
As a side note, do you know if it's best practice to let the server side (in this case, NodeJS) handle DB requests, or can I do them directly from AngularJS?
Server side. Between your angular app and api you are using RESTful API (I assume) and probably some security layer where DB connections usually using different protocols. I don't even know if you can do DB connection management form Angular with exception probably on couple No-Sql DB's.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.