Can anyone help me solve this issue? I am using Firebase Functions node.js javascript. The first code is working perfectly while the second is showing "ReferenceError: doc is not defined". The only difference between these two codes is that the second one is inside the if clause. Please help.
Code One - Works Perfectly:
const usersRef = admin.firestore().collection('users');
const snapshot = await usersRef
.where('gender', '==', 'Male')
.where('account_live', '==', true)
.where('account_suspended', '==', false)
.get();
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(async (doc) => {
console.log(doc.id, '=>', doc.data());
var uid = doc.get('uid');
await washingtonRef.update({
global: admin.firestore.FieldValue.arrayUnion(
'sorting_inside_update_successfull ' + uid
),
});
});
Code Two - Not Working:
var docBefore = change.before;
var docAfter = change.after;
const global_list = docAfter.get('global').length;
console.log('GLOBAL ARRAY SIZE .' + global_list);
if (global_list < 50) {
const usersRef = admin.firestore().collection('users');
const snapshot = await usersRef
.where('gender', '==', 'Male')
.where('account_live', '==', true)
.where('account_suspended', '==', false)
.get();
if (snapshot.empty) {
console.log('No matching documents.');
return;
}
snapshot.forEach(async (doc) => {
console.log(doc.id, '=>', doc.data());
var uid = doc.get('uid');
console.log('USER_UID_INSIDE : ' + user_uid);
const updateRef = admin
.firestore()
.collection('query_sorted')
.doc(user_uid);
await updateRef.update({
global: admin.firestore.FieldValue.arrayUnion(
'sorting_inside_successfull ' + uid
),
});
});
} else {
return null;
}
Please Help.