i am new android beginner and try to deploy firebase functions but show some error how to solve this problem plz help me.
Firebase database structure
User Table
- Users
- user_id
- device_token : user_device_token
- Name : user_name
- user_id
Notification Table
- notifications
- to_user_id
- notification_id
- from : from_user_id
- notification_id
- to_user_id
Index.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
/*
* 'OnWrite' works as 'addValueEventListener' for android. It will fire the function
* everytime there is some item added, removed or changed from the provided 'database.ref'
* 'sendNotification' is the name of the function, which can be changed according to
* your requirement
*/
exports.sendNotification = functions.database.ref('/notifications/{user_id}/{notification_id}').onWrite(event => {
/*
* You can store values as variables from the 'database.ref'
* Just like here, I've done for 'user_id' and 'notification'
*/
const user_id = event.params.user_id;
const notification_id = event.params.notification_id;
console.log('We have a notification from : ', user_id);
/*
* Stops proceeding to the rest of the function if the entry is deleted from database.
* If you want to work with what should happen when an entry is deleted, you can replace the
* line from "return console.log.... "
*/
if(!event.data.val()){
return console.log('A Notification has been deleted from the database : ', notification_id);
}
/*
* 'fromUser' query retreives the ID of the user who sent the notification
*/
const fromUser = admin.database().ref(`/notifications/${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);
/*
* The we run two queries at a time using Firebase 'Promise'.
* One to get the name of the user who sent the notification
* another one to get the devicetoken to the device we want to send notification to
*/
const userQuery = admin.database().ref(`/Users/${from_user_id}/Name`).once('value');
const deviceToken = admin.database().ref(`/Users/${user_id}/device_token`).once('value');
return Promise.all([userQuery, deviceToken]).then(result => {
const userName = result[0].val();
const token_id = result[1].val();
/*
* We are creating a 'payload' to create a notification to be sent.
*/
const payload = {
notification: {
title : "New Friend Request",
body: `${userName} has sent you request`,
icon: "default",
}
};
/*
* Then using admin.messaging() we are sending the payload notification to the token_id of
* the device we retreived.
*/
return admin.messaging().sendToDevice(token_id, payload).then(response => {
console.log('This was the notification Feature');
});
});
});
});
**cmd show error **
C:\Users\TahirAliAwan\Desktop\Function>firebase deploy === Deploying to 'videochat-96f75'... i deploying functions Running command: npm --prefix %RESOURCE_DIR% run lint functions@ lint C:\Users\TahirAliAwan\Desktop\Function\functions eslint . C:\Users\TahirAliAwan\Desktop\Function\functions\index.js 61:11 warning Avoid nesting promises promise/no-nesting 84:14 warning Avoid nesting promises promise/no-nesting 84:69 error Each then() should return a value or throw promise/always-return ✖ 3 problems (1 error, 2 warnings) npm ERR! Windows_NT 10.0.10586 npm ERR! argv "C:\Program Files\nodejs\node.exe" "C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js" "--prefix" "C:\Users\TahirAliAwan\Desktop\Function\functions" "run" "lint" npm ERR! node v6.11.5 npm ERR! npm v3.10.10 npm ERR! code ELIFECYCLE npm ERR! functions@ lint:
eslint .
npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the functions@ lint script 'eslint .'. npm ERR! Make sure you have the latest version of node.js and npm installed. npm ERR! If you do, this is most likely a problem with the functions package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! eslint . npm ERR! You can get information on how to open an issue for this project with: npm ERR! npm bugs functions npm ERR! Or if that isn't available, you can get their info via: npm ERR! npm owner ls functions npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! C:\Users\TahirAliAwan\Desktop\Function\npm-debug.log Error: functions predeploy error: Command terminated with non-zero exit code1
userid
twice? Also remove the .then
function you won't get an error – Peter Haddad.then(response => { console.log('This was the notification Feature');
? – Peter Haddad