2016-12-11 8 views
0
import { Injectable } from '@angular/core'; 

@Injectable() 
export class UserService { 

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

    getUsers(){ 
    //Mlab URL 
    //Example: mongodb://user:[email protected]:29038/database_name 
    } 

} 

こんにちは、私は角度2サービスのユーザーをMLABのデータベースから取得する方法を探しています。どのように私はそれらを受け取り、結果を繰り返すことができますか? よろしく、アプリのフォルダでMlabでAngular 2サービスでデータを取得する方法

+0

AngularJS(別名角2)を使用するmLabのMEANスタックチュートリアルを試してみてください。https://devcenter.heroku.com/articles/mean-apps-restful-api –

答えて

0
  1. は、新しいフォルダを作成して、「サーバー」
  2. インサイド「サーバ」フォルダには、2つの新しいフォルダを作成し、「モデル」という名前を付けて、「ルート」
  3. で、それに名前を付けますmodelsフォルダはjavabriptファイルを作成し、mlab/mongodbのコレクション名と同じ名前を付けます。例 "datacollection1.js"
  4. インサイドdatacollection1.jsは、次のコードを貼り付けます。

    のconstマングース=が( 'マングース')を必要とします。

    constスキーマ= mongoose.Schema;

    CONST datacollection1Schema =新しいスキーマ({ varone:文字列、 vartwo:文字列、 varthree:文字列、 varfour:文字列 })。

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

  5. インサイド "ルート" フォルダのファイルを作成して名 "api.js"

  6. CONSTは=は( '発現')を必要と表現api.jsに次のコードを貼り付けます。 constルータ= express.Router(); const mongoose = require( 'mongoose'); const Datacollection1class =必須( '../ models/datacollection1');

    const db = "mongodb://:@ ds155201.mlab.com:< 12345> /"; mongoose.Promise = global.Promise; mongoose.connect(if(err){ console.log( "Error:" + err); } }); mongoose.connect(db、function(err)) Datacollection1class.find({}) .exec;

    //すべてのデータ router.get( '/ datacollection1class'、関数(REQ、RES){ はconsole.log( "datacollection1class要求を取得")を取得します(関数(ERR、datacollection1class){ IF(ERR){ はconsole.log( "datacollection1classの取得エラーが発生しました。");} 他{res.json(datacollection1class); }}) })。

    // ID router.get( '/ datacollection1class /:ID' でデータを取得する、機能(REQ、RES){ はconsole.log( "SIGLE文書の要求を取得します"); Datacollection1class.findById(REQ .params.id) .exec(関数(ERR、datacollection1classsingle){ IF(ERR){ はconsole.log( "エラーdatacollection1classsingleを取り出す。");他 } {RES。json(datacollection1classsingle); } }) }); VAR newDatacollection1classsingle =新しいDatacollection1class();

    //は、単一の新しいdocumemt router.post( '/ datacollection1classsingle'、機能(REQ、RES){ はconsole.log( 'ポストデータセット')を追加します。 newDatacollection1classsingle.varone = req.body.varone; newDatacollection1classsingle.vartwo = req.body.vartwo; newDatacollection1classsingle.varthree = req.body.varthree; newDatacollection1classsingle.varfour = req.body.varfour; newDatacollection1classsingle.save(関数(エラー、insertedDatacollection1classsingle){ if(err){ console.log( 'エラー保存新しいroデータ: '+ err); } else { res.json(insertedDatacollection1classsingle); } }); });

    //更新既存の文書 router.put( '/ hatacollection1class /:ID'、関数(REQ、RES){ はconsole.log( 'ビデオを更新'); Datacollection1class.findByIdAndUpdate(req.params。 ID、 {$セット:{ varone:req.body.varone、 vartwo:req.body.vartwo、 varthree:req.body.varthree、 varfour:req.body.varfour }}、 { new:true }、 関数(err、updatedDatacollection1classsingle){ if(err){ res.send( "Datacollection1classをIDで更新中にエラーが発生しました:" + err); } else { res.json(updatedDatacollection1classsingle); } }); }); Datacollection1class.findByIdAndRemove(req.params;、関数(REQ、RES){ はconsole.log( 'datacollection1classを削除'):

    //文書 router.delete( 'ID/datacollection1class /' を削除します。 ID、関数(ERR、deletedDatacollection1classsingle){ IF(ERR){ res.send( "IDによってDatacollection1class削除エラー:" + ERR); }他{ res.json(deletedDatacollection1classsingle); }})。 });

  7. コマンドプロンプトに移動し、角度CLIを使用してクラスを作成します。お使いのマシンにAngular CLIがインストールされていることを確認してください。アプリのフォルダ

    NGで、コマンドプロンプトでタイプは、クラスdatacollection1class

を生成し、エンターキーを押します。これにより、 "datacollection1class.ts"という名前のファイルが作成されます。

  1. 内部のdatacollection1class。ts次のコードを入力します。

    エクスポートクラスDatacollection1クラス{ _id:string; varone:string; vartwo:string; varthree:string; varfour:string; }

  2. すべて設定されています。今すぐあなたのコマンドプロンプトで:

    ngのあなたのコマンドプロンプトに続いて

  3. を構築:ブラウザへ

    ノードサーバ

  4. 移動し、「http://localhost:4200/api/datacollection1class」を実行するか、またはあなたが持っている場合文書のID「http://localhost:4200/api/datacollection1class/59305a02734d1d5068f2414e」または郵便配達員を使用して実行している

Feel free to ask questions. upvote if you liked the help.

関連する問題