2017-09-26 3 views
2

MongoDB StitchサービスをAngularで試してみましたが、これまでのところサービスを利用できませんでした。しかし、私がサービスに接続できる唯一の方法は、AWSでホストされているjsライブラリをhtmlページに組み込むことです。mongodb-stitchライブラリをAngular 4で使用

利用可能なmongodb-stitch npmパッケージがあり、mongodbチュートリアルのサンプルページがあります。しかし、これは純粋なJSライブラリ(TSのサポートなし)であり、いくつかの方法を試してきました(requireを使って、libの型をインストールする(利用できません)、@typesを使って)無駄にしました。

誰でもNg4でこれを試しましたか?あなたはサービスを作成する 'mongodb-stitch'パッケージを使用するためにあなたがやったステップを持つことが大好きです。

答えて

1

ない問題は、それが2ヶ月前に頼まれた考えると、まだ関連しているかどうかを確認してくださいとにかく...

あなたが指摘したように何のTSがあなたを結合がないので、あなたがパッケージをインストールする npm install --save mongodb-stitch を使用してすることができます例えば、任意の

としてステッチライブラリを宣言することができます。

declare var stitch: any; 

    export class MyService implements OnInit { 
    db; 

    client; 

    ngOnInit() { 
     this.client = new stitch.StitchClient('<check the stitch app page for proper value>'); 
     this.db = this.client.service('mongodb', 'mongodb-atlas').db('<the db name goes here>'); 
     this.client.login(); 
    } 

    save() { 
     this.db.collection('<collection name>').insertOne({key : 'value'}).then(() => console.log("All done")); 
    } 

    } 
+0

どこかのWebでコーディング例がありますか? import、app.module.ts? –

+0

Stitch APIについては、[公式ドキュメント](https://docs.mongodb.com/stitch/)を使用しています。他のすべてはちょうど角度です。具体的なものは何もありません。前述したように、TypeScriptバインディングはありません。ステートメントを使用できるようにするには '' var stitch:anyを宣言します。 – neta

1

もう1つの答えは、StitchClientの新しいインスタンスをインスタンス化することを提案します。これは、MongoDBがOfficial API Documentationで明示的にアドバイスしたものです。その理由はファクトリメソッドがあるからです。だから、(mongodb-stitchをインストールした後)、次のコードは、あなたがcomponent.ts

import { Component, OnInit } from "@angular/core"; 
import { StitchClientFactory } from "mongodb-stitch"; 

let appId = 'authapp-****'; 

@Component({ 
selector: "app-mongo-auth", 
templateUrl: "./mongo-auth.component.html", 
styleUrls: ["./mongo-auth.component.css"] 
}) 

export class MongoAuthComponent implements OnInit { 

mClient; 

ngOnInit() { 
    this.mClient = StitchClientFactory.create(appId); 
} 

に始めるのに役立つだろうそして、あなたはそのような

gLogin(){ 
this.mClient.then(stitchClient => { 
    stitchClient.authenticate("google"); 
}) 
グーグル

でサインインを実装するとして、あなたが好きな目的のためにこれを使用することができます
関連する問題