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

The name of the picture


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





If you use this.myArray in snapshot forEach what's happen?
– Paresh Gami
6 hours ago





@PareshGami undefined.
– Jeremaine Osia
6 hours ago




1 Answer
1



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





Cannot read property 'push' of undefined
– Jeremaine Osia
5 hours ago





Have you declared myArray to globally?
– Paresh Gami
5 hours ago





yup, I declared the myArray as global var
– Jeremaine Osia
5 hours ago





Can you please show me how you declared that?
– Paresh Gami
5 hours ago





I updated my post. thanks.
– Jeremaine Osia
4 hours ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Blogger: Contempo template have managed to get the posts to post in full but there is still a “snippet” of the post at the bottom of each full post