我尝试在 Angular 项目上使用 pouchDb 和 CouchDb 使用 RemoteDb 对象登录,但收到错误 ERROR TypeError: this.remoteDatabase.login is not a function。下面是代码片段。
import { Injectable, EventEmitter } from '@angular/core';
import PouchDB from 'pouchdb';
@Injectable({
providedIn: 'root'
})
export class PouchDBService {
private isInstantiated: boolean;
private localPouchDb: any;
private remoteDatabase: any;
private listener: EventEmitter<any> = new EventEmitter();
public constructor() {
if (!this.isInstantiated) {
this.localPouchDb = new PouchDB('testdb');
this.isInstantiated = true;
}
PouchDB.plugin(require('pouchdb-authentication'));
}
public sync(remoteDbUrl: string) {
this.remoteDatabase = new PouchDB(remoteDbUrl, {skip_setup: true});
this.remoteDatabase.login('test_admin', 'test_password').then(function () {
this.localPouchDb.sync(this.remoteDatabase, {
live: true,
retry: true
}).on('change', change => {
this.listener.emit(change);
});
}).catch(function(error) {
console.error('Login error' + error);
});
}
我知道这是一个老问题并且已得到解决。但不知怎的,我仍然看到这个错误。有人可以提供一些指导吗? 以下是我的 package.json 详细信息。
{
"name": "test-app",
"version": "0.0.0"
"description": "test app",
"main": "main.js",
"private": true,
"scripts": {
"ng": "ng",
"start": "npm-run-all -p ng:serve electron:serve",
"build": "npm run electron:tsc && ng build",
"build:dev": "npm run build -- -c dev",
"build:prod": "npm run build -- -c production",
"ng:serve": "ng serve -o",
},
"dependencies": {
"@angular/animations": "^6.0.6",
"@angular/cdk": "^6.3.1",
"@angular/common": "^6.0.6",
"@angular/compiler": "^6.0.6",
"@angular/core": "^6.0.6",
"@angular/forms": "^6.0.6",
"@angular/http": "^6.0.6",
"@angular/material": "^6.3.0",
"@angular/platform-browser": "^6.0.6",
"@angular/platform-browser-dynamic": "^6.0.6",
"@angular/router": "^6.0.6",
"@types/pouchdb": "^6.3.2",
"core-js": "^2.5.7",
"hammerjs": "^2.0.8",
"install": "^0.12.0",
"jquery": "^3.3.1",
"moment": "^2.22.2",
"npm": "^6.1.0",
"popper.js": "^1.14.3",
"pouchdb": "^7.0.0",
"pouchdb-authentication": "^1.1.3",
"pouchdb-browser": "^7.0.0",
"rxjs": "^6.2.1",
"rxjs-compat": "^6.2.1",
"zone.js": "^0.8.26"
},
"devDependencies": {
"@angular-devkit/build-angular": "^0.6.8",
"@angular/cli": "^6.0.8",
"@angular/compiler-cli": "^6.0.6",
"@angular/language-service": "^6.0.6",
"@ngx-translate/core": "^10.0.2",
"@ngx-translate/http-loader": "^3.0.1",
"@types/jasmine": "^2.8.8",
"@types/jasminewd2": "^2.0.3",
"@types/node": "^10.3.6",
"codelyzer": "^4.4.2",
"jasmine-core": "^3.1.0",
"jasmine-spec-reporter": "^4.2.1",
"karma": "^2.0.4",
"karma-chrome-launcher": "^2.2.0",
"karma-coverage-istanbul-reporter": "^2.0.1",
"karma-jasmine": "^1.1.2",
"karma-jasmine-html-reporter": "^1.1.0",
"protractor": "~5.3.0",
"ts-node": "^7.0.0",
"tslint": "^5.10.0",
"typescript": "^2.7.2",
"wait-on": "^2.1.0",
"webdriver-manager": "^12.0.6"
}
}
最佳答案
尝试
import PouchAuth from 'pouchdb-authentication'
PouchDB.plugin(PouchAuth)
Github 中指出了相同的错误并在那里解决了
https://github.com/pouchdb-community/pouchdb-authentication/issues/211#issuecomment-359470265
关于angular - Typescript + pouchdb远程登录不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51226268/