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 );