正在使用feathers-chat-angle代码作为指导将feathersjs-client集成到一个有角度的前端中。后端启动并运行:MongoDB,Mongoose,express/feathers服务器。能够从前端检索数据,在显示响应数据时卡住了。羽毛反应目前未使用。警告beginner@work...
正在研究可观察量,但总是遇到:错误:未捕获(承诺(:类型错误:this.dataService.issues$(...(。管。。。不是函数。
基本上玩过.map((和.subscribe((,同样不是函数错误。提前感谢。
编写 myNg 组件
代码import { Component, OnInit, ChangeDetectionStrategy } from '@angular/core';
import { Observable, pipe } from 'rxjs';
import { map } from 'rxjs/operators';
import { Issue } from '../../issue.model';
import { Router } from '@angular/router';
import { DataService } from '../../services/data.service';
//import { Paginated } from '@feathersjs/feathers';
@Component({
selector: 'app-list',
templateUrl: './list.component.html',
styleUrls: ['./list.component.css'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ListComponent implements OnInit {
issues$: Observable<Issue[]>;
constructor(private dataService: DataService) {
//something goes wrong here !!!
this.issues$ = this.dataService.issues$();
}
ngOnInit() {
}
}
代码数据服务:
import { Injectable } from '@angular/core';
import { FeathersService } from './feathers.service';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private feathers: FeathersService) {}
issues$() {
let results = this.feathers.service('issues').find();
console.log(results); //Works records retrieved
return this.feathers
.service('issues')
.find();
}
}
羽毛服务:
import { Injectable } from '@angular/core';
import feathers from '@feathersjs/feathers';
import socketio from '@feathersjs/socketio-client';
import io from 'socket.io-client';
@Injectable({
providedIn: 'root'
})
export class FeathersService {
private client = feathers();
private socket = io('http://localhost:3030');
constructor() {
this.client
.configure(socketio(this.socket))
};
//expose services
public service(name: string) {
return this.client.service(name);
}
}
看起来find
函数根据feathersjs文档返回Promise
:
服务方法必须使用 async/await 或返回 Promise,并具有以下参数
在这种情况下,您需要使用如下所示from
运算符将您的承诺作为可观察量返回:
import { from } from 'rxjs';
const observable = from(promise);
在代码中实现:
代码数据服务:
import { Injectable } from '@angular/core';
import { from } from 'rxjs';
import { FeathersService } from './feathers.service';
@Injectable({
providedIn: 'root'
})
export class DataService {
constructor(private feathers: FeathersService) {}
issues$() {
let results = this.feathers.service('issues').find();
console.log(results); //Works records retrieved
return from(this.feathers
.service('issues')
.find());
}
}