0

I am following a tutorial to deploy a function to firebase. when I deploy I get an unknown error whereas the tutor doesn't. I have looked through this line for line and it's exact. Can anyone else shed any light on this?

// The Cloud Functions for Firebase SDK to create Cloud Functions and   setup triggers.
const functions = require('firebase-functions');

// The Firebase Admin SDK to access the Firebase Realtime Database. 
 const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
const ref = admin.database().ref


exports.sendPushNotification =        functions.database.ref('/posts/{postId}/question').onWrite(event => {
const payload = {
notification: {
  title: 'A Question has been posted',
  body: 'Check out the question posted ',
  badge: '1',
  sound: 'default',
}
};
return admin.database().ref('fcmToken').once('value').then(allToken => {
if (allToken.val()) {
    const token = Object.keys(allToken.val());
    return admin.messaging().sendToDevice(token, payload).then(response => {

  });
};
});
});
2
  • Your code deploys successfully for me. Can you post the text for the deploy error you are getting? Commented Jan 6, 2018 at 16:37
  • Ah, Thank you for your reply @BobSnyder I was getting an error each time I ran the code when I ran it to get the error message for you it ran successfully. I didn't change a thing in it. I did, however, add a fcmToken post in my database, but I am not sure if that has anything to do with it because the tutor didn't have it in when he ran the code. again thank you for your time with it. Commented Jan 6, 2018 at 16:44

1 Answer 1

1

Cloud Functions expects a returned Promise or value. Add a return for the case where there is no token value found in the database:

exports.sendPushNotification = functions.database.ref('/posts/{postId}/question').onWrite(event => {
  const payload = {
    notification: {
      title: 'A Question has been posted',
      body: 'Check out the question posted ',
      badge: '1',
      sound: 'default',
    }
  };
  return admin.database().ref('fcmToken').once('value').then(allToken => {
    if (allToken.val()) {
      const token = Object.keys(allToken.val());
      return admin.messaging().sendToDevice(token, payload).then(response => {

      });
    } else {
      return null; // <= ADDED
    }
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

Excellent this makes sense. Thank you I will add that into my code. Again thank you for your time on this. All the best

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.