如何使用Mlab在Angular 2服务中获取数据

时间:2016-12-11 19:53:10

标签: javascript node.js typescript mlab

import { Injectable } from '@angular/core';

@Injectable()
export class UserService {

  constructor() { 
    console.log("Users service initialized");
  }

  getUsers(){
    //Mlab URL
    //Example: mongodb://user:password@ds129038.mlab.com:29038/database_name
  }

}

嗨,我正在寻找从MLAB数据库中获取角色2服务的用户的方法。我怎么能收到它们并迭代结果? 的问候,

1 个答案:

答案 0 :(得分:0)

  1. 在您的app文件夹中创建一个新文件夹并将其命名为" server"
  2. 内部"服务器"文件夹创建两个新文件夹并命名" models"和" routes"
  3. 在模型文件夹中创建一个javascript文件,并将其命名为mlab / mongodb中的集合名称。示例" datacollection1.js"
  4. 在datacollection1.js内部粘贴以下代码:

    const mongoose = require(' mongoose');

    const Schema = mongoose.Schema;

    const datacollection1Schema = new Schema({     varone:String,     vartwo:String,     varthree:String,     varfour:String });

    module.exports = mongoose.model(' datacollection1',herodataSchema,' datacollection1class');

  5. 内部"路线"文件夹创建一个文件并将其命名为" api.js"

  6. 将以下代码粘贴到api.js

    const express = require(' express'); const router = express.Router(); const mongoose = require(' mongoose'); const Datacollection1class = require(' ../ models / datacollection1');

    const db =" mongodb://:@ ds155201.mlab.com:< 12345> /&#34 ;; mongoose.Promise = global.Promise; mongoose.connect(db,function(err){     if(错误){         console.log("错误:" +错误);     } });

    //获取所有数据 router.get(' / datacollection1class',function(req,res){     console.log("获取datacollection1class&#34的请求;);     Datacollection1class.find({})     .exec(function(err,datacollection1class){         if(错误){             console.log("检索datacollection1class时出错。");         } else {             res.json(datacollection1class);         }     }) });

    //按id检索数据 router.get(' / datacollection1class /:id',function(req,res){     console.log("获取sigle文档请求");     Datacollection1class.findById(req.params.id)     .exec(function(err,datacollection1classsingle){         if(错误){             console.log("检索datacollection1classsingle时出错。");         }         其他         {             res.json(datacollection1classsingle);         }     }) });

    //添加一个新的documemt router.post(' / datacollection1classsingle',function(req,res){     console.log('发布数据集');     var newDatacollection1classsingle = new Datacollection1class();     newDatacollection1classsingle.varone = req.body.varone;     newDatacollection1classsingle.vartwo = req.body.vartwo;     newDatacollection1classsingle.varthree = req.body.varthree;     newDatacollection1classsingle.varfour = req.body.varfour;     newDatacollection1classsingle.save(function(err,insertedDatacollection1classsingle){         if(错误){             console.log('错误保存新的英雄数据:' +错误);         } else {             res.json(insertedDatacollection1classsingle);         }     }); });

    //更新现有文档 router.put(' / hatacollection1class /:id',function(req,res){     console.log('更新视频');     Datacollection1class.findByIdAndUpdate(req.params.id,     {         $ set:{             varone:req.body.varone,             vartwo:req.body.vartwo,             varthree:req.body.varthree,             varfour:req.body.varfour         }     },     {         新:真     },     function(err,updatedDatacollection1classsingle){         如果(ERR){             res.send("错误通过id更新Datacollection1class:" + err);         } else {             res.json(updatedDatacollection1classsingle);         }     }); });

    //删除文件 router.delete(' / datacollection1class /:id',function(req,res){     console.log('删除datacollection1class');     Datacollection1class.findByIdAndRemove(req.params.id,function(err,deletedDatacollection1classsingle){         if(错误){             res.send("错误按id删除Datacollection1class:" + err);         } else {             res.json(deletedDatacollection1classsingle);         }     }); });

  7. 转到命令提示符并使用angular CLI创建一个类。确保您的计算机上安装了Angular CLI。在应用程序文件夹中输入命令提示符

    ng生成类datacollection1class

  8. 然后按Enter键。这将创建一个名为" datacollection1class.ts"

    的文件
    1. 在datacollection1class.ts内部输入以下代码:

      export class Datacollection1class {     _id:string;     varone:string;     vartwo:string;     varthree:string;     varfour:string; }

    2. 你们都已经准备好了。现在在命令提示符下键入:

      ng build

    3. 然后在命令提示符下键入:

      节点服务器

    4. 转到浏览器并运行" http://localhost:4200/api/datacollection1class"或者如果你有文件的身份证,那么" http://localhost:4200/api/datacollection1class/59305a02734d1d5068f2414e"或者使用邮递员运行它

    5.   

      随意提问。如果你喜欢这个帮助,请给予支持。