-1

I have two mongoose schemas running in on my server end. I would like to add two $http.get request in my app.js and eventually display two tables from my collection in MongoDB on a webpage. Only one get function is called without errors.

server.js

//Data Schema
var tempSchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "temperature"});

var humiditySchema = new mongoose.Schema({
    topic: String,
    message: Number,
    when: Date
}, {collection: "humidity"});

var temperature =mongoose.model('temperature', tempSchema);
var humidity =mongoose.model('humidity', humiditySchema);

app.js

app.controller("FormController", function ($http, $scope){
    $http.get("/api/temperature")
        .then(function (response) {
            $scope.temperatures = response.data;
        });
})

app.controller("FormController", function ($http, $scope){
    $http.get("/api/humidity")
        .then(function (response) {
            $scope.humiditys = response.data;
        });
})

Also thinking of how I can display both collections on the webpage. Using ng-repeat. Unfortunately I cannot paste my HTML code here.

I would appreciate any help I can get. Thanks

11
  • 4
    You have defined FormController twice. Commented Dec 22, 2016 at 20:18
  • Do the indexes match up for the data that should go together? so temperatures[0] would go with humiditys[0]? Commented Dec 22, 2016 at 20:21
  • @jbrown I should remove the one them ? Commented Dec 22, 2016 at 20:28
  • You suppose to put ajax calls in the service and not the controller Commented Dec 22, 2016 at 20:28
  • @ProfessorAllman Sorry sir, I'm fairly new to the platform please can you be a little more explicit. Commented Dec 22, 2016 at 20:29

1 Answer 1

0

Another way you could handle the $http requests is by creating an Angular Factory.

angular.module('myApp.services',[])

add.factory('ApiService', function($http) {
    return {
        getHumidity: function() {
            return $http.get("/api/humidity");
        },
        getTemperature: function() {
            return $http.get("/api/temperature");
        }
    }
})

Then inside your controller, you should do the following (Note that you must inject the factory as a dependency for the controller)

angular.module('myApp.controllers',[])
    .controller("FormController", function (ApiService, $scope){
         function getHumidity() {
            var promise = ApiService.getHumidity();
            promise.then(
                function(response) {
                    $scope.humiditys = response.data;
                },
                function(errorPayload) {
                    console.log(errorPayload);
                });
        };

        function getTemperature() {
            var promise = ApiService.getTemperature();
            promise.then(
                function(response) {
                     $scope.temperatures = response.data;
                },
                function(errorPayload) {
                    console.log(errorPayload);
                });
        };

        getHumidity();
        getTemperature();

    })

then where you define your angular App (app.js in most of the cases):

angular.module('myApp', ['myApp.controllers','myApp.services'])
    .run(...)
    .config(...)
    ...
Sign up to request clarification or add additional context in comments.

13 Comments

Do I create the angular factory as a separate JS file?
functionality speaking is the same. But in terms of best practices you better create a new file.
Okay I just added created a separate JS file and added the first part of the code. Is there anything else I need to add? Just trying to be sure I got it right.
Check again, I updated the answer just to be more clear.
Thank you so much for your help. But I must say I think i'm bit confused on where I should place the file above. I would appreciate a little guidance.
|

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.