I am trying to depply functions from node.js to firebase. I am getting the error
Function failed on loading user code. Error message: Error: please examine your function logs to see the error cause:
I have tried to read the documentation but I can't wrap my head around it.
I am very new to node, (like 3 days new.) But I did read that node.js was updated to node 10 from 8. Can someone please tell me if my function logs are incorrect??
const functions = require('firebase-functions');
var admin = require("firebase-admin");
var serviceAccount = require("mydirectory");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "myfirebaseproject.com"
});
exports.sendPushNotifications = functions.https.onRequest((req, res) => {
res.send('Attempting to send push notification')
console.log('LOGGER --- Trying to send push message..');
var uid = 'uidstring'
var fcmToken = 'myFCMToken'
return admin.database().ref('/users/' + uid).once('value', snapshot => {
var user = snapshot.val();
console.log("username is " + user.name);
var payload = {
notification: {
title: 'Push Notification Title',
body: 'Test Notification Message'
}
}
admin.messaging().sendToDevice(fcmToken, payload)
.then(function(response) {
console.log('Succesfully sent message:', response);
console.log(response.results[0].error);
})
.catch(function(error) {
console.log('Error sending message', error);
});
})
})
package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"firebase-functions-test": "^0.2.0"
},
"private": true
}