简体   繁体   English

如何在firestore angularfire的变量中分配快照结果

[英]how to assign the result of snapshot in a variable in firestore angularfire

I just want to check if the email exists then I will get the rest of the doc but I can't seem to assign the result in variable. 我只想检查电子邮件是否存在,那么我将获得其余文档,但似乎无法将结果分配给变量。

this.db.collection('accounts', ref => ref
.where('inviteCode', '==', 'abcd1234')
.get()
.then(querySnapshot => {
  const results = [];
  querySnapshot.forEach(doc => {
    results.push(doc.data());
  });

  this.myArray = results; // this is my variable
})

.catch(function (error) {
  console.log('Error getting documents', error);
}));

I tried using Promise.all(results) but it seems i'm not doing it right. 我尝试使用Promise.all(results),但看来我做得不好。

UPDATE: 更新:

export class FirebaseService {
    preloginRef: AngularFirestoreCollection<PreLogin>;
    myArray: [];

    constructor(private db: AngularFirestore) {
      this.preloginRef = db.collection(this.dbPath);
    }

    codeCheck(inviteCode: string) {
       this.db.collection('accounts', ref => ref
      .where('inviteCode', '==', inviteCode)
      .get()
      .then(querySnapshot => {
         let that = this;
         querySnapshot.forEach(doc => {
            that.myArray.push(doc.data());
         });
    }).catch(function (error) {
      console.log('Error getting documents', error);
    }));

    console.log('result: ' + this.myArray);
    }
}

You can try this snippet. 您可以尝试此代码段。

this.db.collection('accounts', ref => ref
.where('inviteCode', '==', 'abcd1234')
.get()
.then(querySnapshot => {
    let that = this; // Here you have to assign this scope to any variable
    querySnapshot.forEach(doc => {
        that.myArray.push(doc.data());
    });
    console.log(this.myArray);
})

.catch(function (error) {
console.log('Error getting documents', error);
}));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM