- Trying to achieve: Extract data from a separate node via firebase cloud functions
- Process: onCreate on /users/{uid} , which gives me a managedby key, using which I access another /users/uid.
- Error: (ERROR 1):TypeError cannot read property 'name' of null. (UPDATE 1): There were two problems with the code (a) I did not use the $ sign for ${managedby} and the second one was not using a ".then" in the const managerVal. (ERROR 2): New error thrown is : admin.database.ref(..).then is not a function at exports.sendWelcomeEmail... I have read elsewhere that we are not supposed to use admin in betwen the function. What is the alternative then? ERROR 3: Look at ERROR 2 update below. I think it must be something small that is causing this.
name has a value. Is there something fundamentaly wrong here? (cleared. read UPDATE 1)
admin.initializeApp(functions.config().firebase); // // Create and Deploy Your First Cloud Functions // // https://firebase.google.com/docs/functions/write-firebase-functions // // exports.helloWorld = functions.https.onRequest((request, response) => { // response.send("Hello from Firebase!"); // }); exports.sendWelcomeEmail = functions.database.ref('/users/{uid}').onWrite(event => { if (!event.data.exists() || event.data.previous.exists()) { return; } var user = event.data.val() var {email} = user var {managedby} = user const managerVal = admin.database().ref(`/users/{managedby}`).on('value', function(snapshot){ const managedByName = snapshot.val().name const managedByMobile = snapshot.val().mobile }); console.log(email) var data = { from: '[email protected]', subject: 'From Your Admin! Please set your Password for your account', html: `<p> hello</p>`, to: email } mailgun.messages().send(data, function (error, body) { console.log(body) }) });
ERROR 2: UPDATE: I have used @faruk's method:
var user = event.data.val();
var email = user.email;
var managedby = user.managedby;
const managerVal = db.ref('/users/' + managedby).on('value', function(snapshot){
console.log("inside the function")
const managerUser = snapshot.val();
if(managerUser)
{
const managedByName = managerUser.name;
const managedByMobile = managerUser.mobile;
}
});
console.log("outside the function")
I have also tried to use .then
var user = event.data.val();
var email = user.email;
var managedby = user.managedby;
const managerVal = db.ref('/users/' + managedby).once('value')
.then((snapshot) => {
console.log("inside the function")
const managerUser = snapshot.val();
if(managerUser)
{
const managedByName = managerUser.name;
const managedByMobile = managerUser.mobile;
}
});
console.log("outside the function")
The problem now is, the "outside the function" part is being fired first and then the "inside the function" part is being fired. Isnt the execution within the function completed first before it moves away? I dont know where the problem is.
Edit : Faruk, I have put (copy pasted) the entire mailer function within the database call as you have show. Something very basic is being missed. Still get this error:
Error 2: UPDATE 2: I have just used .then and the code is this.
var db = admin.database();
if (!event.data.exists() || event.data.previous.exists()) {
return;
}
var user = event.data.val();
var email = user.email;
var managedby = user.managedby;
console.log(managedby)
const managerVal = db.ref('/users/' + managedby).once('value')
managerVal.then((snapshot) => {
const managerUser = snapshot.val()
console.log("inside snapshot")
if (managerUser) {
console.log("deep inside snapshot")
const managedByName = managerUser.name
const managedByMobile = managerUser.mobile
}
})
console.log("outside snapshot")
console.log('managerName = ' + managedByName)
console.log('email = ' + email)
This is how it is featured in the dashboard (given in the screenshot below). Going by how complicated it is getting, I think it must be a real simple solution.


