简体   繁体   English

Angular 服务与 Azure 宇宙

[英]Angular Service with Azure Cosmos

I am really new to Angular.我对 Angular 真的很陌生。 I am trying to create a service which i want to consume in my angular component.我正在尝试创建一个我想在我的 angular 组件中使用的服务。 While doing so i am getting below error.这样做时,我遇到了错误。

Below is my code which i am writing下面是我正在写的代码

import { Injectable } from '@angular/core';
import { HttpClient} from '@angular/common/http';
import { CosmosClient } from '@azure/cosmos';
import {Observable,of} from 'rxjs'

@Injectable({
  providedIn: 'root'
})
export class ApiService {
  
  databaseId='dbName';
  containerId='Container Name';
  constructor() { }

  public async getProjects():Promise<Observable<any>>{
    const endpoint = "https://AAAA.documents.azure.com:443/";
    const key = "==";
    const client = new CosmosClient({ endpoint, key });
    const database = client.database(this.databaseId);
    const container = database.container(this.containerId);

    const querySpec = {query: "SELECT * from c where c.Category=\"Details\""};
    const { resources:items } = await container.items.query(querySpec).fetchAll();
    return of(items);
  }
  
}

Any help is really appreciated.非常感谢任何帮助。

错误图片

There is an exception to every rule, but the use cases for connecting to a DB directly from a web browser are pretty close to zero.每个规则都有一个例外,但是直接从 web 浏览器连接到数据库的用例几乎为零。 By doing so, you lose all fine grained control over what a user can do in your database and the only way to revoke access is to rotate your keys.这样一来,您就失去了对用户在您的数据库中可以做什么的所有细粒度控制,而撤销访问权限的唯一方法是轮换您的密钥。 You may not currently have anything in your database that is sensitive, but it is still considered bad practice.您的数据库中目前可能没有任何敏感的内容,但这仍然被认为是不好的做法。

For this reason, the CosmosDB library is compatible with NodeJS as a server-side framework.出于这个原因,CosmosDB 库与作为服务器端框架的 NodeJS 兼容。 Whether or not it works with front end frameworks like Angular or React are incidental.它是否适用于 Angular 或 React 等前端框架是偶然的。 There are some large changes in how Angular compiles projects in version 9, and it looks like the Cosmos client is not compatible with the new Ivy compiler. Angular 在版本 9 中编译项目的方式发生了一些较大的变化,看起来 Cosmos 客户端与新的 Ivy 编译器不兼容。

You do have a couple options here.你在这里有几个选择。

  1. (recommended) Use an API layer between your database and your front end. (推荐)在数据库和前端之间使用 API 层。 You can use Node to keep it within Javascript.您可以使用 Node 将其保存在 Javascript 中。 If you are running on Azure, there are services like Azure Functions that can make this even easier to implement securely, or you can run it from the same App Service, VM, or whatever hosting solution you are using.如果您在 Azure 上运行,则有诸如 Azure 之类的服务可以使这更容易安全地实施,或者您可以从相同的应用程序服务、VM 或您正在使用的任何托管解决方案中运行它。
  2. Disable the new Ivy compiler.You can do this by adding aot: false in your angular.json 禁用新的 Ivy 编译器。您可以通过在 angular.json 中添加aot: false来执行此操作

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

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