user10111849
user10111849

Reputation:

Unable to deploy cloud functions

when I deployed my functions following error came up I tried everything but I cannot solve the problem I am writing the function for push notification i think i write the code correctly but i am not sure its correct or not

This is the error which I receive on CMD

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.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\000\AppData\Roaming\npm-cache\_logs\2018-07- 
20T15_42_18_516Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit 
code1

This is the LOG File

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm- 
cli.js',
1 verbose cli   '%RESOURCE_DIR%',
1 verbose cli   'run',
1 verbose cli   'lint' ]
2 info using [email protected]
3 info using [email protected]
4 verbose run-script [ 'prelint', 'lint', 'postlint' ]
5 info lifecycle functions@~prelint: functions@
6 info lifecycle functions@~lint: functions@
7 verbose lifecycle functions@~lint: unsafe-perm in lifecycle true                        
9 verbose lifecycle functions@~lint: CWD: C:\Users\000\fb- 
functions\%RESOURCE_DIR%
10 silly lifecycle functions@~lint: Args: [ '/d /s /c', 'eslint .' ]
11 silly lifecycle functions@~lint: Returned: code: 1  signal: null
12 info lifecycle functions@~lint: Failed to exec lint script
13 verbose stack Error: functions@ lint: `eslint .`
13 verbose stack Exit status 1
13 verbose stack     at EventEmitter.<anonymous> (C:\Program 
Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:304:16)
13 verbose stack     at EventEmitter.emit (events.js:182:13)
13 verbose stack     at ChildProcess.<anonymous> (C:\Program 
Files\nodejs\node_modules\npm\node_modules\npm- 
lifecycle\lib\spawn.js:55:14)
13 verbose stack     at ChildProcess.emit (events.js:182:13)
13 verbose stack     at maybeClose (internal/child_process.js:961:16)
13 verbose stack     at Process.ChildProcess._handle.onexit 
(internal/child_process.js:248:5)
14 verbose pkgid functions@
15 verbose cwd C:\Users\000\fb-functions
16 verbose Windows_NT 10.0.10586
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program 
Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "--prefix" 
"%RESOURCE_DIR%" "run" "lint"
18 verbose node v10.4.1
19 verbose npm  v6.1.0
20 error code ELIFECYCLE
21 error errno 1
22 error functions@ lint: `eslint .`
22 error Exit status 1
23 error Failed at the functions@ lint script.
23 error This is probably not a problem with npm. There is likely 
additional logging output above.
24 verbose exit [ 1, true ]

This is the code which i wite in index.js

'use strict'

 const functions = require('firebase-functions');

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




 exports.sendNotification=
 functions.database.ref('/Notifications/${receiver_id}/{notification_id}')
               .onWrite(event =>{
               const receiver_id = event.params.receiver_id;
               const notification_id = event.params.notification_id;
               console.log('We have a Notifications to send to :', 
 receiver_id);
             if (!event.data.val())
             {
                return console.log('A Notifications has been deleted from 
  the database', notification_id);
               }

            const deviceToken = 

   admin.database().ref(`/Users/${receiver_id}/device_token`)
  .once('value');

          return deviceToken.then(response =>
          {
            const token_id = result.val();
            const payload =
            {
              notifications;
              title: "Friend Request",
              body: "You have a New Friend",
              icon: "default"

              return admin.messaging().sendToDevice(token_id, payload)
              .then(response =>{
                console.log('This was the notification feature.');
              });
            };
          });
        });

Upvotes: 0

Views: 161

Answers (1)

Jen Person
Jen Person

Reputation: 7566

What version of Cloud Functions are you using? If you are on 1.0 or later, then there are a couple of things to resolve in the code. For one, the guide indicates that Realtime Database .onWrite triggers pass two parameters: change and context. Also, the path shouldn't have a "$" in it. Also, the parameters object appears to encompassing the second half of all of the code. And in that payload it says "notifications;", but I'm not sure what that's supposed to be. Seems like it should match the payload shown in the guide. There may yet be other errors I didn't catch. If it were me, I'd probably try getting a simpler function to successfully deploy, and then add to it piece by piece.

exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
    .onWrite((change, context) => {
        const receiver_id = context.params.receiver_id;
        const notification_id = context.params.notification_id;
        console.log('We have a Notifications to send to :', receiver_id);
        if (!change.after.val()) {
            return console.log('A Notifications has been deleted from the database', notification_id);
        }
        const deviceToken = admin.database().ref(`/Users/${receiver_id}/device_token`)
            .once('value');
        return deviceToken.then(response => {
            const token_id = result.val();
            const payload = {
                notification: {
                    title: 'Friend Request',
                    body: 'You have a New Friend',
                    icon: 'default'
                }
            };
            return admin.messaging().sendToDevice(token_id, payload)
                .then(response => {
                    console.log('This was the notification feature.');
                });
            });

});

Upvotes: 1

Related Questions