akshayejh / Firebase-Device-to-Device-Push-Notification

Source File for Tutorial Video of Firebase Android.
39 stars 28 forks source link

problem with firebase deploy #1

Open leenorshn opened 6 years ago

leenorshn commented 6 years ago

hello, teacher ! i have an issue when i am trying to deploy a function witch is in index.js file, a take all your code witch your publish to github for notification, but i have this issue:warning nesting promise promise/no-nesting error Each the() should return a value or throw promise/always-return. Please help me, I'm using FirebaseCloud messaging 11.0.4 and nodev8.4 for windows. i'm dont understand why my code dont work image

muankit commented 6 years ago

Modify in firebase.json from "npm --prefix $RESOURCE_DIR run lint" to "npm --prefix %RESOURCE_DIR% run lint"

It Worked For Me.

miladxyz commented 6 years ago

On Firebase init step, when it asked "Do you want to use ESLint to catch probable bugs and enforce style?" You just say no It should solve your problem

muankit commented 6 years ago

Got error in log :

My index.js is here

`'use strict'

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase);

exports.sendNotification = functions.database.ref('/notification/{user_id}/{notification_id}').onWrite(event => {

const user_id = event.params.user_id; const notification = event.params.notification;

console.log("The User Id is : ",user_id);

if(!event.data.val()){

return console.log('A Notification has been deleted from database : ', notification_id);

}

const fromUser = admin.database().ref(/notification/${user_id}/{notification_id}).once(value); return fromUser.then(fromUserResult => {

    const from_user_id = fromUserResult.val().from;
    console.log('You have new notification from : ', from_user_id);

    const userQuery = admin.database().ref(`/Users/${user_id}/${notification_id}`).once('value');
    return userQuery.then(userResult => {

        const userName = userResult.val();
        const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');

        return deviceToken.then (result => {

          const token_id = result.val();

          const payload = {
            notification : {

              title : "Friend Request",
              body : `${userName} has sent you request`,
              icon : "default"
            }
          };

          return admin.messaging().sendToDevice(token_id , payload).then(response => {

            console.log('This was the Notifiaction Feature');
            return null;

          });
        });
  });

}); }); ` logs_error

imandal665 commented 6 years ago

sollution for----

error Each then() should return a value or throw promise/always-return npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! functions@ lint: eslint . npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the functions@ lint script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

try by editing index.js

use this code--

return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');
    return null;
  }).catch(error => {
console.error(error);
res.error(500);
 });

instead of ---

return admin.messaging().sendToDevice(token_id, payload).then(response => {

    console.log('This was the notification Feature');

  });

Show less

Developer-AP commented 6 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

EngAlaa22 commented 6 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

Have you found any solution for this issue??

Atominac commented 5 years ago

exports.helloWorld = functions.database.ref('notification/{id}').onWrite(evt => { const payload = { notification:{ title : 'Message from Cloud', body : 'This is your body', badge : '1', sound : 'default' } };

return admin.database().ref('fcm-token').once('value').then(allToken => {
    if(allToken.val()){
        console.log('token available');
        const token = Object.keys(allToken.val());
        return admin.messaging().sendToDevice(token,payload);
    }else{
       return console.log('No token available');
    }
});

}

----this worked for me instead of: console.log('No token available'); use: return console.log('No token available');

this is because then() should return something

Prakash-ap commented 5 years ago

exports.helloWorld = functions.database.ref('notification/{id}').onWrite(evt => { const payload = { notification:{ title : 'Message from Cloud', body : 'This is your body', badge : '1', sound : 'default' } };

return admin.database().ref('fcm-token').once('value').then(allToken => {
    if(allToken.val()){
        console.log('token available');
        const token = Object.keys(allToken.val());
        return admin.messaging().sendToDevice(token,payload);
    }else{
       return console.log('No token available');
    }
});

}

----this worked for me instead of: console.log('No token available'); use: return console.log('No token available');

this is because then() should return something

this one too worked for me thank you Atominac !!!

atmanad commented 4 years ago

exports.helloWorld = functions.database.ref('notification/{id}').onWrite(evt => { const payload = { notification:{ title : 'Message from Cloud', body : 'This is your body', badge : '1', sound : 'default' } };

return admin.database().ref('fcm-token').once('value').then(allToken => {
    if(allToken.val()){
        console.log('token available');
        const token = Object.keys(allToken.val());
        return admin.messaging().sendToDevice(token,payload);
    }else{
       return console.log('No token available');
    }
});

}

----this worked for me instead of: console.log('No token available'); use: return console.log('No token available');

this is because then() should return something

Worked for me too. Thank you man!

cofound-ind-in commented 3 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

Have you solve this issue.

cofound-ind-in commented 3 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

Hi @Developer-AP Are you able to solve?? I have same issue

Atominac commented 3 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

Hi @Developer-AP Are you able to solve?? I have same issue

Can you please share the complete error and the code snippet giving this error.

leenorshn commented 2 years ago

In my log, I am getting this error! Please help me. It's very important! Thank you! say alap functions firebase console - google chrome 17-07-2018 00_09_31

Hi, please check in your code, you'll find somewhere you are using an object without define it. Your compile is seeing some thing like this undefined.user_id .

Or you can share your code for more details