mongodb - 在 Angular 4 中使用 mongodb-stitch 库

标签 mongodb angular typescript

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

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

有人在 Ng4 上试过这个吗?很想知道您使用“mongodb-stitch”包创建服务的步骤。

最佳答案

另一个答案建议实例化 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);
}

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

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

关于mongodb - 在 Angular 4 中使用 mongodb-stitch 库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46417758/

相关文章:

javascript - 在 mongodb 中通过 _id 删除不起作用

mongodb - 为什么 BsonObjectId::apply(String) 方法第二次不起作用?

javascript - 如何在不获取 ExpressionChangedAfterItHasBeenCheckedError 的情况下对子组件中的 Material 输入使用 ngIf

reactjs - 将 i18next 实例传递给 I18nextProvider 时出现类型错误

reactjs - Material UI Autocomplete 上的 Typescript Equality 问题

java - 如何使用 PojoCodecProvider 忽略 getter/setter

node.js - 使用 Mognodb 和 Node.js 聚合函数返回空白数组值

javascript - 获取数组对象类型脚本中的第一个索引

angular - 使用 Angular 获取 react 时间 - 在第二次单击事件时调用第二个函数

html - 我正在尝试所有选定的数组来更改 Angular 4 的样式