[英]How I can query for specific data using angularjs,nodejs and mongodb?
I am new to nodejs and angularjs and hope I can get some help here. 我是nodejs和angularjs的新手,希望能在这里得到一些帮助。 I am trying to create a small application for learning .I am using angularjs,Mongodb and nodejs.
我正在尝试创建一个用于学习的小型应用程序。我正在使用angularjs,Mongodb和nodejs。 How I can query for a problem based on a specific idea id passed as a parameter using the following given??
如何使用以下给定的参数作为参数传递基于特定想法ID的问题? 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 );
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 您正在尝试进行异步调用,以便在一段时间后可以使用哪些数据。.您需要将其数据放入
$resource
promise中,如下所示
Problem.query({id: 2}).$promise
.then(function(data){
$scope.problem = data;
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.