首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Angular 4中使用mongodb-stitch库

在Angular 4中使用mongodb-stitch库
EN

Stack Overflow用户
提问于 2017-09-26 12:18:18
回答 3查看 1.9K关注 0票数 3

我一直在尝试在角度的MongoDB针迹服务,到目前为止,我能够使用该服务。然而,我可以连接到服务的唯一方法是在html页面上包含托管在AWS中的js库。

有一个mongodb-stitch npm包可用,mongodb教程上有关于如何使用它的示例页面。但这是一个纯JS库(不支持TS ),我尝试了几种方法(使用require、安装库的类型(不可用)、使用@types)都没有用。

有人在Ng4上试过吗?我很想知道您在使用'mongodb-stitch‘包创建服务时所做的步骤。

EN

回答 3

Stack Overflow用户

发布于 2018-02-11 08:35:55

另一个答案建议实例化一个新的StitchClient实例,这是MongoDB在Official API Documentation中明确建议的-而且是有道理的,因为有一个工厂方法可以用于该目的。因此,(在安装mongodb-stitch之后),以下代码将帮助您开始使用component.ts

代码语言:javascript
复制
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);
}

然后,您可以将其用于任何您想要的目的,例如实现与Google的登录

代码语言:javascript
复制
gLogin(){
this.mClient.then(stitchClient => {
  stitchClient.authenticate("google");
})
票数 3
EN

Stack Overflow用户

发布于 2017-12-06 17:48:35

我不确定这个问题是否仍然相关,因为它是两个月前提出的,但不管怎样……

正如您所指出的,您可以使用npm install --save mongodb-stitch来安装该包,并且由于没有TS绑定,您可以将缝合库声明为any

例如:

代码语言:javascript
复制
    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"));
    }

  }
票数 1
EN

Stack Overflow用户

发布于 2018-04-09 06:28:50

前面的答案是函数式的,但我想分享一个使用服务注入的示例。

service.ts

代码语言:javascript
复制
import { Injectable }    from '@angular/core';
import { Jsonp, URLSearchParams } from '@angular/http';

import { StitchClientFactory } from "mongodb-stitch";

import 'rxjs/add/operator/map';
@Injectable()
export class Service {  
  constructor(private jsonp: Jsonp) { }    
  client;
  connect(){
    this.client = new StitchClientFactory.create("App ID");  // Slitch apps > Clients > App ID
    this.client.then(stitchClient => stitchClient.login())
      .then((stitchClient) => console.log('logged in as: ' + stitchClient))
      .catch(e => console.log('error: ', e));
  }
  all() {
    this.connect();     
    return this.client.then(stitchClient => {
      let db = stitchClient.service('mongodb', 'mongodb-atlas').db("database Name"); // Slitch apps > mongodb-atlas > Name database.Collection
      let itemsCollection = db.collection('name collection'); // Slitch apps > mongodb-atlas > Name database.Collection        
      console.log(itemsCollection.find().execute());
      return itemsCollection.find().execute();                  
    })
     .then(result => {return result})
     .catch(e => console.log('error: ', e));          
   }
 }

在创建前一个文件之后,您必须创建一个模块来接收数据,因此:

module.ts

代码语言:javascript
复制
import { Component, OnInit, Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { StitchClientFactory } from "mongodb-stitch";
import { Service }          from 'service'; // previous code

declare var stitch: any;

@Component({  
  template: '
    <ul class="demo-list-icon mdl-list">
      <li class="mdl-list__item" *ngFor="let item of data | async">
         <span class="mdl-list__item-primary-content">
           <i class="material-icons mdl-list__item-icon">{{propiedad.nombre}}</i>            
         </span>
      </li>
   </ul>
  '  
 })
 export class MainComponent implements OnInit {  
   data: Observable<[]>;
   constructor(private Service: service) {
   }
   ngOnInit() {     
     this.propiedades = this.Service.all();
   }  
}

重要的是,你不能忘记在module.ts初始化声明中添加服务。

mongodb Atlas

mongodb-stitch vía NPM

Documentation mongoDB Stitch.

好的!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46417758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档