javascript - TypeError: Cannot read property 'uid' of undefined - Stack Overflow

This is my first javascript function for firebase. TypeError: Cannot read property 'uid' of u

This is my first javascript function for firebase.

TypeError: Cannot read property 'uid' of undefined

I got this error in firebase cloud function.How to resolve it?

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

    exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
      .onCreate(event => {
        const uid = event.params.uid;
        admin.database().ref(`users/${uid}/referred_by`)
          .once('value').then((data) => {
            var referred_by_somebody = data.val();
            if (referred_by_somebody) {
              var moneyRef = admin.database().ref(`/users/${uid}/earned`);
              moneyRef.transaction((current_value)=> {
                return (current_value || 0) + 10;
              });
            }
            return console.log('reddem updated')
          }).catch(error => {
            console.log("Got an error: ",error);
          });
      });

This is my first javascript function for firebase.

TypeError: Cannot read property 'uid' of undefined

I got this error in firebase cloud function.How to resolve it?

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

    exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
      .onCreate(event => {
        const uid = event.params.uid;
        admin.database().ref(`users/${uid}/referred_by`)
          .once('value').then((data) => {
            var referred_by_somebody = data.val();
            if (referred_by_somebody) {
              var moneyRef = admin.database().ref(`/users/${uid}/earned`);
              moneyRef.transaction((current_value)=> {
                return (current_value || 0) + 10;
              });
            }
            return console.log('reddem updated')
          }).catch(error => {
            console.log("Got an error: ",error);
          });
      });
Share Improve this question edited Aug 22, 2018 at 6:30 piet.t 11.9k21 gold badges44 silver badges55 bronze badges asked Apr 29, 2018 at 11:09 Shweta ChauhanShweta Chauhan 6,9916 gold badges42 silver badges59 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 7

The cloud functions were updated, therefore you need to change this:

 exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
  .onCreate(event => {
    const uid = event.params.uid;

into this:

 exports.grantSignupReward = functions.database.ref('/users/{uid}/last_signin_at')
  .onCreate(snap,context) => {
    const uid = context.params.uid;

more info here:

https://firebase.google./docs/functions/beta-v1-diff#realtime-database

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745217389a4617090.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信