AngularFire .subscribe => console.log 在使用 map 或 flatMap 后返回 undefined

标签 angular rxjs angularfire observable

我对 AngularFire、Firebase、rxjs 等还很陌生。

谁能告诉我为什么以下内容会从 console.log 返回“undefined”?

    this.af.database.object('/teams/' + this.id)
        .map((data) => {
            data.teamName = Teams[data.team];
        })
        .subscribe((data) => {
            console.log(data)
        })

Teams 是一个枚举。红色 = 1,蓝色 = 2...

我知道问题出在 .map 中,就好像我将其删除并仅记录 x 或记录枚举查找一样工作正常..

这工作正常(并解析为红色):

    this.af.database.object('/teams/' + this.id)
        .subscribe((x) => {
            console.log(Teams[x.team]);
        })

这样做(解析一个 firebase observable):

    this.af.database.object('/teams/' + this.id)
        .subscribe((x) => {
            console.log(x);
        })

我还尝试过在列表上使用 flatMap 而不是在对象上进行映射。

如有任何帮助,我们将不胜感激。

最佳答案

因为 getmap 方法的返回都是可观察的,而不是 json。

 .map((data) => {
            data.teamName = Teams[data.team];
        })

您将 data 变量视为 json,但事实并非如此。所以你的变量变得未定义。因为没有 data.Teamname

这样的东西

引用:https://angular.io/docs/ts/latest/guide/server-communication.html#!#fetch-data

http.get method returns an Observable of HTTP Responses (Observable) from the RxJS library and map is one of the RxJS operators.

关于AngularFire .subscribe => console.log 在使用 map 或 flatMap 后返回 undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40442243/

相关文章:

angularjs - 具有 OAuth 浏览器重定向方法的 AngularFire 无限 $digest 循环

angular - amcharts 4 光标和鼠标定位不正确

javascript - 从导入的库调用 Angular Component 方法

javascript - 如何在 RxJS 中按功能组合函数( f(g(h)))?

Angular 2 Observable Interval 锁定 UI

angularjs - 是否可以在 firebase 中获取唯一值的数组?

javascript - 类型错误 : Cannot read property 'length' of null

javascript - 如何去抖动内部组件的@Output?

javascript - 在 Angular 中使用 shareReplay(1) - 仍然调用 http 请求?

javascript - $firebase 在 AngularJS 中未定义