简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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