简体   繁体   English

如何使用angularjs,nodejs和mongodb查询特定数据?

[英]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.

相关问题 如何使用SocketsJs在nodejs mongodb和angularjs中获取实时数据? - how to get realtime data in nodejs mongodb and angularjs using SocketsJs? Node.js + angularjs + mysql:如何使用路由/视图将查询数据呈现到angularjs - Nodejs + angularjs + mysql : How to render query data to angularjs using route/view 使用nodejs和angularjs更新mongodb - Updating mongodb using nodejs and angularjs 如何使用angularjs在mongodb中显示已经保存的图像? - How can I display an already saved image in mongodb using angularjs? 如何根据特定数据在Mongodb中进行查询? - How to query in Mongodb based on specific data? 需要在mongodb中保存html的文本框数据,并使用angularjs,expressjs,nodejs在浏览器中显示保存的数据 - Need to save text box data of html in mongodb and display the saved data in browser using angularjs, expressjs , nodejs 如何使用AngularJS在Bootstrap模式下将数据重置为默认值? - How can I reset data to default in Bootstrap modal using AngularJS? 我如何使用Angularjs资源获取数据? - How i can get Data using Angularjs Resource? 如何使用AngularJS在HTML滚动条中加载数据? - How Can I Load Data On Scroll In Html using AngularJS? 如何使用AngularJs + MVC加载jVectorMap数据? - How can I load jVectorMap data using AngularJs + MVC?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM