最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论