博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[MEAN Stack] First API -- 3. Select by ID with Mongoose and Express
阅读量:5111 次
发布时间:2019-06-13

本文共 2108 字,大约阅读时间需要 7 分钟。

 

Mongoose allows you to easily select resources by ID from your MongoDB. This is an important aspect to creating an API.

Server.js

'use strict';var expres = require('express');var mongoose = require('mongoose');mongoose.connect('mongodb://localhost/simple');var cors = require("cors");var personSchema = {    firstName:String,    lastName:String,    email:String};//create a person model, and rename db as peoplevar Person = mongoose.model('Person', personSchema);var app = expres();app.use(cors());app.get('/people', function(request, response){    Person.find().select("firstName").exec(function(err, data) {        response.json(200, data);    })});app.get('/people/:id', function(request, response){    Person.findById(request.params.id, function(err, data) {       response.send(data);    });});app.listen(3000);

 

app.js:

/** * Created by Answer1215 on 12/9/2014. */'use strict';function MainCtrl(PeopleService) {    var vm = this;    vm.people = [];    vm.selectedPerson = {};        vm.getPeople = PeopleService.getPeople().then(function(response) {        vm.people = response.data;    });    vm.selectPerson = function(person) {        PeopleService.getSelectedPerson(person).then(function(response) {            vm.selectedPerson = response.data;            vm.selectedPerson.fullName = response.data.firstName + " " + response.data.lastName;        });    }}function PeopleService($http) {    var PeopleService = {};    PeopleService.getPeople = function() {         return $http.get('http://localhost:3000/people');    }    PeopleService.getSelectedPerson = function(person) {        return $http.get('http://localhost:3000/people/'+person._id);    }    return PeopleService;}angular.module('app',[])    .controller('MainCtrl', MainCtrl)    .service('PeopleService', PeopleService);

 

index.html

    
  • {
    {person.firstName}}

{
{vm.selectedPerson.fullName}}

{
{vm.selectedPerson.email}}

 

转载于:https://www.cnblogs.com/Answer1215/p/4154582.html

你可能感兴趣的文章
将DataSet(DataTable)转换成JSON格式(生成JS文件存储)
查看>>
javascript日常学习小记
查看>>
Objective-C 学习笔记(Day 2)
查看>>
如何使用PHP显示在线Word文档
查看>>
Discuz常见小问题-如何设置163邮箱注册验证
查看>>
IO流的基础与小型项目
查看>>
机器学习之--KNN算法简单实现
查看>>
【转】NoSQL小故事:单服务器如何应付每秒75万次查询(2)
查看>>
[Unity Shader] 3D模型的简单属性
查看>>
壁纸推荐2018
查看>>
JQuery对象转dom ,dom转jQuery
查看>>
jquery 异步请求
查看>>
2018-09-25
查看>>
微信小程序开发---小程序对接Django---6
查看>>
[mysql] mysql批量操作时性能优化
查看>>
jQuery下拉菜单
查看>>
python习题:对比两个字典内容哪里不一样 并把不 一样的key和value打印出来
查看>>
不定长数组:vector
查看>>
pig grunt shell详解
查看>>
hadoop Shell命令详解
查看>>