如何在 CouchDB 中使用 angular2+ 创建数据库?
How to create database using angular2+ in CouchDB?
我正在尝试使用 angular2+ 在 couchdb 中创建一个数据库。单击应创建数据库的按钮时,我创建了一个按钮。
但我收到错误消息。
"ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":401,"statusText":"Unauthorized","url":"http://127.0.0.1:5984/myproject","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://127.0.0.1:5984/myproject: 401 Unauthorized","error":{"error":"unauthorized","reason":"You are not a server admin."}}"
at resolvePromise (zone.js:831)
at eval (zone.js:741)
at eval (zone.js:757)
at ZoneDelegate.invoke (zone.js:391)
at Object.onInvoke (core.js:4760)
at ZoneDelegate.invoke (zone.js:390)
at Zone.run (zone.js:150)
at eval (zone.js:889)
at ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:4751)
有人可以帮我怎么做吗?我已经添加了下面的代码。
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
interface Resp {
id: string;
ok: boolean;
rev: string;
}
@Component({
selector: "app-databasse",
templateUrl: "./databasse.component.html",
styleUrls: ["./databasse.component.css"]
})
export class DatabasseComponent {
constructor(private http: HttpClient) {}
books = [
{ id: 1, name: "Core Java" },
{ id: 2, name: "Angular 2" },
{ id: 3, name: "Hibernate" }
];
async createDatabase(): Promise<Resp> {
return new Promise<Resp>((resolve, reject) => {
this.http.put<Resp>(`http://127.0.0.1:5984/myproject`, this.books).subscribe(
res => {
console.log('created document: ' + res.id)
resolve(res)
},
err => {
reject(err)
}
)
})
}
}
------template file------
<p>
<button (click)="createDatabase()">createDatabase</button>
</p>
您的代码并没有真正尝试创建数据库,而是尝试将文档(书籍)添加到现有数据库中。创建数据库和插入大量文档不能在单个 HTTP 调用中完成。我将回答有关如何从 Angular.
创建 CouchDB 数据库的问题
对于 creating a database, CouchDB Server Administrator privileges are required. Therefore you need to submit corresponding credentials (username and password) together with your HTTP request. There exists different ways of authentication, but you could start with basic authentication,使用 CouchDB 进行身份验证的快速简单方法。
在您的 Angular 代码中,您将 HttpClient.put()
method to send a PUT request to CouchDB. This request should also include an options
object that contains some required HttpHeaders
与 属性 withCredentials: true
一起使用。这个 options
对象可以按如下方式创建。您显然需要用实际值替换“:”。
createHttpOptions(): any {
let httpHeaders = new HttpHeaders();
httpHeaders = httpHeaders.set('Accept', 'application/json');
httpHeaders = httpHeaders.set('Content-type', 'application/json');
httpHeaders = httpHeaders.set('Authorization', 'Basic ' + btoa("<username>:<password>"));
return { headers: httpHeaders, withCredentials: true };
}
请看CouchDBService
from the open source project Koia。它更详细地说明了如何从 Angular 与 CouchDB 通信。 class 实际上还包含一个 createDatabase
方法。
To make your HTTP requests work, make sure to configure CORS by changing [http] and [cors] entries within the configuration file $COUCHDB_HOME/etc/local.ini as explained here.
我正在尝试使用 angular2+ 在 couchdb 中创建一个数据库。单击应创建数据库的按钮时,我创建了一个按钮。 但我收到错误消息。
"ERROR Error: Uncaught (in promise): HttpErrorResponse: {"headers":{"normalizedNames":{},"lazyUpdate":null},"status":401,"statusText":"Unauthorized","url":"http://127.0.0.1:5984/myproject","ok":false,"name":"HttpErrorResponse","message":"Http failure response for http://127.0.0.1:5984/myproject: 401 Unauthorized","error":{"error":"unauthorized","reason":"You are not a server admin."}}"
at resolvePromise (zone.js:831)
at eval (zone.js:741)
at eval (zone.js:757)
at ZoneDelegate.invoke (zone.js:391)
at Object.onInvoke (core.js:4760)
at ZoneDelegate.invoke (zone.js:390)
at Zone.run (zone.js:150)
at eval (zone.js:889)
at ZoneDelegate.invokeTask (zone.js:423)
at Object.onInvokeTask (core.js:4751)
有人可以帮我怎么做吗?我已经添加了下面的代码。
import { Component } from "@angular/core";
import { HttpClient } from "@angular/common/http";
interface Resp {
id: string;
ok: boolean;
rev: string;
}
@Component({
selector: "app-databasse",
templateUrl: "./databasse.component.html",
styleUrls: ["./databasse.component.css"]
})
export class DatabasseComponent {
constructor(private http: HttpClient) {}
books = [
{ id: 1, name: "Core Java" },
{ id: 2, name: "Angular 2" },
{ id: 3, name: "Hibernate" }
];
async createDatabase(): Promise<Resp> {
return new Promise<Resp>((resolve, reject) => {
this.http.put<Resp>(`http://127.0.0.1:5984/myproject`, this.books).subscribe(
res => {
console.log('created document: ' + res.id)
resolve(res)
},
err => {
reject(err)
}
)
})
}
}
------template file------
<p>
<button (click)="createDatabase()">createDatabase</button>
</p>
您的代码并没有真正尝试创建数据库,而是尝试将文档(书籍)添加到现有数据库中。创建数据库和插入大量文档不能在单个 HTTP 调用中完成。我将回答有关如何从 Angular.
创建 CouchDB 数据库的问题对于 creating a database, CouchDB Server Administrator privileges are required. Therefore you need to submit corresponding credentials (username and password) together with your HTTP request. There exists different ways of authentication, but you could start with basic authentication,使用 CouchDB 进行身份验证的快速简单方法。
在您的 Angular 代码中,您将 HttpClient.put()
method to send a PUT request to CouchDB. This request should also include an options
object that contains some required HttpHeaders
与 属性 withCredentials: true
一起使用。这个 options
对象可以按如下方式创建。您显然需要用实际值替换“:”。
createHttpOptions(): any {
let httpHeaders = new HttpHeaders();
httpHeaders = httpHeaders.set('Accept', 'application/json');
httpHeaders = httpHeaders.set('Content-type', 'application/json');
httpHeaders = httpHeaders.set('Authorization', 'Basic ' + btoa("<username>:<password>"));
return { headers: httpHeaders, withCredentials: true };
}
请看CouchDBService
from the open source project Koia。它更详细地说明了如何从 Angular 与 CouchDB 通信。 class 实际上还包含一个 createDatabase
方法。
To make your HTTP requests work, make sure to configure CORS by changing [http] and [cors] entries within the configuration file $COUCHDB_HOME/etc/local.ini as explained here.