0

I am new to nodejs and angularjs and hope I can get some help here. I am trying to create a small application for learning .I am using angularjs,Mongodb and nodejs. How I can query for a problem based on a specific idea id passed as a parameter using the following given?? I have the following :I created the problem service and an idea service:

app.factory('Problem', 
          ['$rootScope', '$resource',
  function ($rootScope,   $resource) {

    return $resource($rootScope.apiBaseURL + '/problems/:id', {id:'@id'}, {
        update: {
            method: 'PUT'
        }
    });
}]);

I have also the problem controller:

app.controller('ProblemController', [
           '$rootScope','$scope', 'Problem',
  function ($rootScope,$scope, Problem) {
    //$scope.htmlVariable = 'Explain why you selected this problem… What is the need that you are trying to fulfill?(Please make sure to limit your answer to 140 Words)';
     $scope.problem = Problem.query();
  }]

Back-end:

use strict';

// modules dependencies
var mongoose = require('mongoose'),
    Problem     = mongoose.model('Problem');

/**
 * create problem
 */
exports.create = function (req, res) {
  var newProblem = new Problem(req.body);

  newProblem.save(function(err) {
    if (err) return res.status(400).send(err)

    res.json(newProblem);
  });
};

/**
 * update problem
 */
exports.update = function (req, res) {
  //TODO check token
};


/**
 *  get problem by id
 */
exports.getById = function (req, res) {

  Problem.findById(req.params.id, function (err, problem) {
    if (err) return res.status(400).send(err)

    if (problem) {
      res.send(problem);
    } else {
      res.status(404).send('Problem not found')
    }
  });
};

/**
 *  get all problems
 */
exports.getAll = function (req, res) {

  Problem.find(function (err, problems) {
    if (err) return res.status(400).send(err)

    res.send(problems);
  });
};
    );

Problem schema:

'use strict';

// modules dependencies
var mongoose = require('mongoose'),
    Schema   = mongoose.Schema;

// model
var ProblemSchema = new Schema({

  description: {
    type     : String,
    unique   : false,
    required : false
  },
  creator: {
    type     : Schema.Types.ObjectId,
    unique   : false,
    required : true,
    ref      : 'User'
  },
  idea: {
    type     : Schema.Types.ObjectId,
    unique   : false,
    required : true,
    ref      : 'Idea'
  }
});

mongoose.model('Problem', ProblemSchema);

Routes:

// problem routes
  var problemController = require('../controllers/problemController');
  router.post   ('/problems',     authController.isBearerAuthenticated, problemController.create );
  router.put    ('/problems/:id', authController.isBearerAuthenticated, problemController.update );
  router.get    ('/problems/:id', authController.isBearerAuthenticated, problemController.getById);
  router.get    ('/problems',     authController.isBearerAuthenticated, problemController.getAll );

1 Answer 1

1

You are trying to make async call which data will be available after some time..you need to get its data inside $resource promise like below

Problem.query({id: 2}).$promise
.then(function(data){ 
    $scope.problem = data; 
});
Sign up to request clarification or add additional context in comments.

4 Comments

How do I query for a specific problem based on the idea id ??
Problem.query() is working fine and it is retrieving all problems in the problem schema
How I can inject idea id so that exports.getById = function (req, res) will query for problem based on the idea id not problem id ??I have 2 schemas in Mongodb one for problem and another for Idea.
@Abadi it seems like you handled that on server side.. honestly saying I didn't work on MEAN.

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.