Angular方法以未定义而不是JSON对象的形式返回 [英] Angular method returns as undefined rather than JSON object

查看:116
本文介绍了Angular方法以未定义而不是JSON对象的形式返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在身份验证提供程序中有一个getUser方法(在Ionic 2中工作):

I have a getUser method within an Authentication provider (working in Ionic 2):

  getUser(uid: string): any {
    var toReturn;

        firebase.database().ref('/userProfile').orderByKey().equalTo(uid).once("value", function(snapshot){
          toReturn = snapshot.val();
          console.log("getUser = " + JSON.stringify(toReturn));
        });

        return toReturn;
      }

它从firebase中检索用户对象. 只要存在console.log,它就可以正确输出输出,并保存在toReturn变量中.

It retrieves a user object from firebase. It gets output correctly with the console.log providing it exists and is saved in the toReturn variable.

下面是ItemDetailPage类,但是当我记录相同方法的值时,它返回未定义的

Below is the ItemDetailPage class however when I log the value of the same method, it returns undefined.

import { Component } from '@angular/core';
import { NavParams} from 'ionic-angular';
import { Auth } from '../../providers/auth/auth';

@Component({
  templateUrl: 'build/pages/item-detail/item-detail.html',
  providers: [Auth]
})
export class ItemDetailPage {
  private title;
  private description;
  private author;

  constructor(private navParams: NavParams, private _auth: Auth) {
    this.title = this.navParams.get('item').title;
    this.description = this.navParams.get('item').description;
    this.author = _auth.getUser(this.navParams.get('item').author);
    console.log("item-detail.ts = " + JSON.stringify(this.author));
    //_auth.getUser('123');
  }

}

我是Angular的新手,并在学习过程中尝试学习,所以这可能是我很想念的很简单的事情.但是,为什么不返回/传递回JSON对象?我需要它来利用从数据库查询的值.

I'm new to Angular and trying to learn as I go along, so this is probably something quite simple I'm missing out. However, why does the JSON object not get returned/passed back? I need it to utilise the values queried from the database.

谢谢

推荐答案

您缺少的是Firebase方法正在异常运行.因此,它确实返回了一个承诺而不是一个值.这就是为什么您将返回的值作为回调获取的原因.

What you are missing is that the Firebase method is running assincronously. So, it does return a promise instead of a value. That's why you get the returned value as a callback.

您应该做的是:在您的服务中返回Firebase Promise(或一个新的Firebase Promise,以防您想要在将数据返回给控制器之前对数据做一些事情).然后,您想在控制器中创建一个回调函数.

What you should do is: return the firebase promise in your service (or a new one in case you want to do something with the data before returning it to the controller). Then you want to create a callback function into the controller.

return firebase.database().ref('/userProfile').orderByKey().equalTo(uid).once("value", function(snapshot){});

// Controller

_auth.getUser(this.navParams.get('item')).then(function(snapshot){
    ...
});

您应该研究诺言在普通javascript中的工作方式:

You should take a look into how promises work in plain javascript:

http://andyshora.com/promises-angularjs-explained-as- cartoon.html

然后以NG2样式使用它们

And then how they are used in the NG2 style

http://coenraets.org/blog /2016/02/angular2-ionic2-data-services-promises-observables/

这篇关于Angular方法以未定义而不是JSON对象的形式返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆