简体   繁体   English

Angular 7应用程序中的MongoDB Stitch

[英]MongoDB Stitch in Angular 7 application

I am trying to add connectivity to MongoDB Stich in my Angular 7 application but app fails with error: 我正在尝试在Angular 7应用程序中添加与MongoDB Stich的连接性,但应用程序失败并出现以下错误:

bson.browser.esm.js:453 Uncaught ReferenceError: global is not defined bson.browser.esm.js:453未捕获的ReferenceError:未定义全局

I am using Angular 7.2.12 and have installed mongodb-stitch-browser-sdk v 4.3.2 我正在使用Angular 7.2.12并安装了mongodb-stitch-browser-sdk v 4.3.2

I am using the code found on MongoDB website under DB Stitch application client setup. 我正在使用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');
  }
}

The app fails in ngOnInit() with the error: 该应用程序在ngOnInit()中失败,并显示以下错误:

bson.browser.esm.js:453 Uncaught ReferenceError: global is not defined at Module../node_modules/bson/dist/bson.browser.esm.js (bson.browser.esm.js:453) at webpack_require (bootstrap:78) at Module../node_modules/mongodb-stitch-core-sdk/dist/esm/index.js (index.js:1) at webpack_require (bootstrap:78) at Module../node_modules/mongodb-stitch-browser-core/dist/esm/index.js (index.js:1) at webpack_require (bootstrap:78) at Module../node_modules/mongodb-stitch-browser-sdk/dist/esm/index.js (index.js:1) at webpack_require (bootstrap:78) at Module../src/app/alfabetisk/alfabetisk.component.ts (main.js:95) at webpack_require (bootstrap:78) bson.browser.esm.js:453未捕获的ReferenceError:在webpack_require (bootstrap:bootstrap: boot.strap :bootstrap:)上未在Module ../ node_modules / bson / dist / bson.browser.esm.js(bson.browser.esm.js:453)中定义全局变量 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)在模块../ SRC /应用/ alfabetisk / alfabetisk.component.ts(main.js:95)在webpack_require(自举:78)

Add this to polyfills.ts for Angular 6+ 将其添加到Angular 6+的polyfills.ts中

/** Add global to window */
(window as any).global = window;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM