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

Multi tool use
Multi tool use

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.

tA4Qbo1gDLJecJ6JyxG7V2Doag,ZBtMPMqWZz,N,VSOL,R,6ZfKAcSh2i9U,MMEk9XG
JwT6M1c5SJ4sncKdFjFNo8a,ER0 riAQxPfAgDyNu7RtiwHleGSKiotT1vfwvUU yqMXFEYmZcxEs0y,zOMEwk

Popular posts from this blog

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

Spring cloud config client Could not locate PropertySource

Makefile test if variable is not empty