This firebase function keeps track data changes of a firestore collection and push it to the Existing contact of a CRM.
// LISTENER
exports.listenData = functions.https.onRequest(async(request, response) => {
// Keep track of profile collection
db.collection('profile').onSnapshot(async(querySnapshot) => {
console.log('change in profile');
let changedDocs = querySnapshot.docChanges();
for (let i = 0; i < changedDocs.length; i ) {
let change = changedDocs[i];
if (!change.doc.exists) {
continue;
}
console.log('change in profile in loop');
let profileDocData = change.doc.data();
let body = {
"customField": {
"subscription_expiration_date": ""
}
};
let user = await admin.auth().getUser(change.doc.id);
// profile
try {
body.customField.subscription_expiration_date = profileDocData.expiration_date;
} catch (error) {
body.customField.subscription_expiration_date = '';
}
console.log("Profile changed by " user.email ? ? "Unknown");
try {
let res = await axios.get('EndPoint URL ?email=' user.email, {
headers: {
"Authorization": "Bearer Token"
}
});
let userId = res.data.contacts[0].id;
axios.put('EndPoint URL /contacts/' userId, body, {
headers: {
"Authorization": "Bearer Token"
}
});
} catch (error) {
console.log('Error on profile');
}
}
});
response.send("Listening...");
});
Output:
Error
Exception from a finished function: Error: There is no user record corresponding to the provided identifier.
I'm confused here! Anyone?
CodePudding user response:
First of all, when you send a response back to the client, the function is closed (that's why you get "a finished function"). So, if you want to monitor changes, you should set up a trigger: https://firebase.google.com/docs/functions/firestore-events.
The error itself says there is no user in the Firebase Authentication section with the id you are providing the admin.auth().getUser
with.