角度-等待此变量具有值,然后执行 [英] Angular - wait for this variable to have value and then execute

查看:52
本文介绍了角度-等待此变量具有值,然后执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ngOnInit()中,第一行代码从本地存储中获取 value ,然后使用该值对数据库中的数据进行过滤

In ngOnInit(), first line of code fetches the value from local storage and then that value is used to filter data from database.

两者都执行,但我没有得到结果.我该如何做类似第二个函数 waits 的操作,以便第一个函数获得价值.

Both executes together and I don't get the result. How can I do something like the second function waits for the first one to get value.

以下是我的 ts 代码:

 ngOnInit() {

    //get id of user
    this.storage.get('loggedInUser').then((val) => {
      console.log('Your user ID is', val);
      this.loggedInusr = val;
    });

    firebase.firestore().collection(`todos`)
  .where("assignTo", "==", this.loggedInusr) // don't get value here, if I hard code all works
  .get()
  .then(querySnapshot => {
      querySnapshot.forEach(function(doc) {
          console.log("assignTo");
          console.log(doc.id, " ===> ", doc.data());
      });
    });

    ...

推荐答案

您可以在第一个过程的处理中移动第二个过程.尝试以下

You could move the 2nd procedure inside the handling of 1st procedure. Try the following

ngOnInit() {
  //get id of user
  this.storage.get('loggedInUser').then((val) => {
    console.log('Your user ID is', val);
    this.loggedInusr = val;

    firebase.firestore().collection(`todos`)
      .where("assignTo", "==", this.loggedInusr) // dont get value here, if i hard code all works
      .get()
      .then(querySnapshot => {
        querySnapshot.forEach(function(doc) {
            console.log("assignTo");
            console.log(doc.id, " ===> ", doc.data());
        });
      });

  });
  .
  .
}

这篇关于角度-等待此变量具有值,然后执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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