user9722951
user9722951

Reputation:

Why can't I deploy this code to firebase functions? I don't understand the error

I'm new to firebase functions - obviously - and I'm trying to test to see if an email in that specific path of the database when created is being used by an account if it isn't being used then change that database value accordingly. Here's the code:

exports.checkEmail = functions.database.ref('/checkEmailExistance')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const email = snapshot.val();
      console.log('Email:', email, context.params.pushId);
      admin.auth().getUserByEmail(email)
      .then(snapshot => {
          const data = snapshot.toJSON()

          return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
      })
});

and the error is:

ERROR: /Users/nathan/Documents/FirebaseFunctionsClipify/functions/src/index.ts:41:7 - Promises must be handled appropriately
ERROR: /Users/nathan/Documents/FirebaseFunctionsClipify/functions/src/index.ts:42:13 - Shadowed name: 'snapshot'

npm ERR! code ELIFECYCLE
npm ERR! errno 2
npm ERR! functions@ lint: `tslint --project tsconfig.json`
npm ERR! Exit status 2
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!     /Users/nathan/.npm/_logs/2019-04-25T16_21_29_696Z-debug.log

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

UPDATE:

I changed the code so the errors shouldn't be produced again, but still got the same error:

exports.checkEmail = functions.database.ref('/checkEmailExistance')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const email = snapshot.val();
      console.log('Email:', email, context.params.pushId);
      return admin.auth().getUserByEmail(email)
      .then(snap => {
          const data = snap.toJSON()

          return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
      })
    });

Upvotes: 0

Views: 110

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317362

The second error is telling you that you redefined an existing variable called snapshot. Note that snapshot is defined by at the top level of your function callback, then again in the then callback. The second instance is "shadowing" the first, which is a potential error in your code. Just name the second variable something different.

The first lint error is telling you that you have an unhandled promise in your code. You can fix this by returning the promise from admin.auth().getUserByEmail().then(...):

  return admin.auth().getUserByEmail(email)
  .then(snap => {
      const data = snap.toJSON()

      return admin.database().ref('checkEmailExistance').child(email).set("Nope") 
  })

Upvotes: 1

Related Questions