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

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

 

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}}

 

转载地址:http://jsctx.baihongyu.com/

你可能感兴趣的文章
2018智博会与腾讯“云+未来”峰会重庆站同日揭幕,六大亮点提前连连看
查看>>
为什么Oracle公开嫌弃自家产品MySQL?
查看>>
华为敏捷DevOps实践:如何从Excel管理软件的方式中走出来
查看>>
为什么Python发展得如此之快?
查看>>
使用Spring Cloud Function框架进行面向函数的编程
查看>>
C# 8的Ranges和递归模式
查看>>
大前端时代,如何做好C 端业务下的React SSR?\n
查看>>
基础设施即代码:Terraform和AWS无服务器
查看>>
LCA---Lowest common ancestor
查看>>
新的UWP和Win32应用程序分发模型
查看>>
乔布斯的64周年诞辰,苹果滞销的第N天
查看>>
Apache CarbonData里程碑式版本1.3发布
查看>>
组合式测试:移动测试服务的发展趋势
查看>>
一线:阿里云不做SaaS,那这件事会交给谁?
查看>>
Netflix混沌工程手册Part 2:混沌工程原则
查看>>
Serialize and Deserialize Binary Tree & BST
查看>>
如何打造一流的视觉AI技术
查看>>
Redux异步方案选型
查看>>
干货 | Api 体系架构分享(下)
查看>>
【Java深入学习系列】之值传递Or引用传递?
查看>>