尝试调用CouchDB视图GET API时(URL: http://localhost:5984/testdb/_design/tcViews/_view/get_tc_products?group=true) 获得 404 状态,错误为" 收集' _design'找不到"。
当通过PostMan / SoapUI调用相同的URL时,我能够从couchDB(v1.6.1)获得结果。 CouchDB记录器中没有记录错误。
我尝试编码' _'但是网址中的字符却出现了相同的错误。
我试着搜索这个问题但到目前为止没有运气..
请提出任何建议..
以下是我的服务类 -
export class TestcasesService {
private dbApiHostPort:string ;
private dbName:string;
private productByTcApiUrl:string;
private productByTcAPIQueryUrl:string;
constructor(private http: Http,
@Inject(APP_CONFIG) private config:Config) {
this.dbApiHostPort = config.couchDbHostPort;
this.dbName = config.couchDbDBName;
this.productByTcApiUrl = config.couchDbGetTcProductUrl;
this.productByTcAPIQueryUrl='http://localhost:5984/testdb/_design/tcViews/_view/get_tc_products?group=true';
}
getProductsForTcs(): Observable<ProductResults> {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.get(this.productByTcAPIQueryUrl,options)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
let body = res.json();
return body.data || { };
}
private handleError(error: any) {
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
console.error('An error occurred', error);
return Observable.throw(errMsg);
}
}
这就是它在其中一个组件中使用的方式 -
export class TestcasesFilterComponent implements OnInit {
title = 'Tour of Heroes';
public selectedProduct:ProductResult;
public errorMessage:any ;
public disabled:boolean = false;
public status:{isopen:boolean} = {isopen: true};
public items:ProductResults ;
constructor(private testcasesService: TestcasesService,
private router: Router) { }
ngOnInit() {
let defaultProduct:ProductResult = new ProductResult();
defaultProduct.key ="Select Product";
this.selectedProduct = defaultProduct;
this.testcasesService.getProductsForTcs().subscribe(
products => this.items = products,
error => this.errorMessage = <any>error);
}
onProductSelect(productResult:ProductResult){
this.selectedProduct = productResult;
}
}
这就是我在Chrome控制台中获得的内容 -
解决方案 - 在敲了几天之后,我意识到我的主打错了。它被配置为使用InMemoryBackend服务。
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ELEMENT_PROBE_PROVIDERS,
provide(XHRBackend, { useClass: InMemoryBackendService }), // in-mem server
provide(SEED_DATA, { useClass: InMemoryDataService }) // in-mem server data ]);
评论InMemory服务解决了问题 -
bootstrap(AppComponent, [
HTTP_PROVIDERS,
ELEMENT_PROBE_PROVIDERS,
// provide(XHRBackend, { useClass: InMemoryBackendService }), // in-mem server
// provide(SEED_DATA, { useClass: InMemoryDataService }) // in-mem server data
]);