devs, I'm trying to deploy a simple cloud function to the firebase console, everything working very well (installation of npm & configuration & other stuff...).
then I wrote a simple function in the index.js file :
'use strict'
const functions = require("firebase-functions");
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/notififcation/{user_id}/{notififcation_id}').onWrite(
event =>
{
const user_id = event.params.user_id;
const notififcation_id = event.params.notififcation_id;
console.log('this id is the ' , user_id);
}
):
then, when I wanna deploy it to firebase with this command firebase deploy, This error keeps appearing, this is the error :
C:\Users\nasro\Desktop\oussamaproject\notifyfun\functions\index.js
14:2 error Parsing error: Unexpected token :
? 1 problem (1 error, 0 warnings)
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 l
ging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\nasro\AppData\Roaming\npm-cache\_logs\2021-09-19T21_28_2
892Z-debug.log
events.js:292
throw er; // Unhandled 'error' event
^
Error: spawn npm --prefix "C:\Users\nasro\Desktop\oussamaproject\notifyfun\fun
ions" run lint ENOENT
at notFoundError (C:\Users\nasro\AppData\Roaming\npm\node_modules\firebase
ools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\nasro\AppData\Roaming\npm\node_modules\firebase-
ols\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
at ChildProcess.cp.emit (C:\Users\nasro\AppData\Roaming\npm\node_modules\f
ebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:2
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
Emitted 'error' event on ChildProcess instance at:
at ChildProcess.cp.emit (C:\Users\nasro\AppData\Roaming\npm\node_modules\f
ebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:30:3
at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn npm --prefix "C:\\Users\\nasro\\Desktop\\oussamaproject\\not
yfun\\functions" run lint',
path: 'npm --prefix "C:\\Users\\nasro\\Desktop\\oussamaproject\\notifyfun\\f
ctions" run lint',
spawnargs: []
}
Error: functions predeploy error: Command terminated with non-zero exit code1
so after searching for a solution in firebase documentation and articles, I tried those solutions
solution one: in firebase.json by default:
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
i Modified it to:
"predeploy": [
"npm --prefix \"%RESOURCE_DIR%\" run lint"
]
the error keeps appearing again with the same error message, so I tried solution 2
Solution two
I modified again the file firebase.json to :
"predeploy": [
"npm --prefix \"%RESOURCE_DIR%\" run lint",
"npm --prefix \"%RESOURCE_DIR%\" run build"
]
and the error keeps appearing again and again with the same error message (btw I'm using windows7)
So any solution or suggestions for this error ..
CodePudding user response:
Looks like your function ends with a colon instead of a semicolon. Change it to be like this:
"use strict";
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database
.ref("/notififcation/{user_id}/{notififcation_id}")
.onWrite((change, context) => {
const userId = context.params.user_id;
// const notififcationId = context.params.notififcation_id;
console.log("this id is the ", userId);
}
);
CodePudding user response:
the onWrite
method takes 2 arguments - change
and context
and the params live on the context
object.
could you try the following:
functions.database.ref("/notififcation/{user_id}/{notififcation_id}").onWrite((change, context)=>{
const user_id = context.params.user_id;
const notififcation_id = context.params.notififcation_id;
console.log("this id is the " , user_id);
return null; // <----- please try returning something as well at the end
});
More info here - https://firebase.google.com/docs/functions/database-events#reading_the_previous_value
There also seems to be an issue with the linting hence the linting errors are coming up as per your comment below, perhaps try updating your .eslintrc.js
or .eslintrc.json
file in your project like so:
module.exports = {
root: true,
env: { es6: true, node: true, },
extends: [ "eslint:recommended" ], // <--- remove google from here
rules: { quotes: ["error", "double"], }
};