gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:00:10 25 4
gpt4 key购买 nike

我一直在尝试 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com