簡體   English   中英

如何在firestore angularfire的變量中分配快照結果

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

我只想檢查電子郵件是否存在,那么我將獲得其余文檔,但似乎無法將結果分配給變量。

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);
}));

我嘗試使用Promise.all(results),但看來我做得不好。

更新:

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);
    }
}

您可以嘗試此代碼段。

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