数据未通过离子2

时间:2018-03-07 09:34:15

标签: angular firebase ionic-framework rxjs angularfire2

我正在研究离子2项目。在此我希望数据基于 Firebase 的日期传递。

我从 Firebase 获取提供商的数据,但我无法将其发送到其他网页

我在提供者中的功能如下

getTodaysQuestions() {
  var today = new Date();

  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();
  var day = "";
  var month = "";
  if (dd < 10) {
    day = "0" + dd;
  } else {
    day = "" + dd;
  }

  if (mm < 10) {
    month = "0" + mm;
  } else {
    month = "" + mm;
  }
  var d = day + month + yyyy;
  console.log(d);

  var fbstring = "/Daily/" + d;
  console.log(fbstring);
  var qstring = "/Questions/q/";

  this.afd.list(fbstring).valueChanges().subscribe(
    m => {
      //for each m, get actual questions from fb
      console.log(m);
      for (var x in m) {
        console.log(x);
        var str = qstring + m[x];
        console.log(str);

        console.log(this.questions);
        this.afd.list(str).valueChanges().subscribe(
          n => {
            console.log(n);
            this.questions = n;
            //this.questions.push(n);
            console.log(this.questions);
            return this.questions;


          }
        );
      }
    });
} 

当我在家里调用这个函数时,我得到了未定义的值。

mydata: any;


this.mydata = this.data.getTodaysQuestions();
console.log(this.mydata);

2 个答案:

答案 0 :(得分:1)

在您的功能中尝试此代码

return this.afd.list(fbstring).valueChanges().map(changes => {
    return changes.map(m => { 
        //for each m, get actual questions from fb
        console.log(m);
        for (var x in m) {
            console.log(x);
            var str = qstring + m[x];
            console.log(str);

            return this.afd.list(str).valueChanges();

        }
    });
});

并在home.ts

mydata: Observable<any>;

this.mydata = this.data.getTodaysQuestions();
this.mydata.subscribe(data => {
    console.log(data);
})

答案 1 :(得分:0)

从我在您的代码中看到的,您有2个Observables订阅,因此您应该处理该异步数据流。

getTodaysQuestions(): Observable<any> {
  var today = new Date();

  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();
  var day = "";
  var month = "";
  if (dd < 10) {
    day = "0" + dd;
  } else {
    day = "" + dd;
  }

  if (mm < 10) {
    month = "0" + mm;
  } else {
    month = "" + mm;
  }
  var d = day + month + yyyy;
  console.log(d);

  var fbstring = "/Daily/" + d;
  console.log(fbstring);
  var qstring = "/Questions/q/";

  return this.afd.list(fbstring).valueChanges().subscribe(
    m => {
      //for each m, get actual questions from fb
      console.log(m);
      for (var x in m) {
        console.log(x);
        var str = qstring + m[x];
        console.log(str);

        console.log(this.questions);
       return this.afd.list(str).valueChanges().subscribe(
          n => {
            console.log(n);
            this.questions = n;
            //this.questions.push(n);
            console.log(this.questions);
            return this.questions;


          }
        );
      }
    });
}

现在getTodaysQuestions()返回一个Observable。现在您应该能够订阅它:

this.data.getTodaysQuestions().subscribe((data: any) => {
   this.mydata = data;
   console.log(this.mydata);
});