Angular 7应用程序中的MongoDB Stitch

Tom*_*mmy 0 mongodb angular mongodb-stitch

我正在尝试在Angular 7应用程序中添加与MongoDB Stich的连接性,但应用程序失败并出现以下错误:

bson.browser.esm.js:453未捕获的ReferenceError:未定义全局

我正在使用Angular 7.2.12并安装了mongodb-stitch-browser-sdk v 4.3.2

我正在使用MongoDB网站上DB Stitch应用程序客户端设置下的代码。

import { Component, OnInit } from '@angular/core';
import { Stitch, RemoteMongoClient, AnonymousCredential} from 'mongodb-stitch-browser-sdk'

@Component({
  selector: 'app-alfabetisk',
  templateUrl: './alfabetisk.component.html',
  styleUrls: ['./alfabetisk.component.css']
})
export class AlfabetiskComponent implements OnInit {

  constructor() { }

  ngOnInit() {
    const client = Stitch.initializeDefaultAppClient('app-id');
  }
}
Run Code Online (Sandbox Code Playgroud)

该应用程序在ngOnInit()中失败,并显示以下错误:

bson.browser.esm.js:453未捕获的ReferenceError:在webpack_require(引导程序: 78)位于Module ../ node_modules / mongodb-stitch-core-sdk / dist / esm / index.js(index.js:1)位于webpack_require(bootstrap:78)位于Module ../ node_modules / mongodb-stitch-browser -core / dist / esm / index.js(index.js:1)在webpack_require(bootstrap:78)在Module ../ node_modules / mongodb-stitch-browser-sdk / dist / esm / index.js(index.js :1)在webpack_require(引导:78)的Module ../ src / app / alfabetisk / alfabetisk.component.ts(main.js:95)处的webpack_require(引导:78)

小智 5

将其添加到Angular 6+的polyfills.ts中

/** Add global to window */
(window as any).global = window;
Run Code Online (Sandbox Code Playgroud)