Saturday 15 April 2017

TypeError: event.data.key is not a function - Firebase Cloud Functions

I get this error message: log

This is my data structure: structure

And this is my code (part of):

exports.lotteryTickets = functions.database.ref('/lottery/ticketsneedstobeprocessed/{randomID}').onWrite(event => {
    let ticketsBoughtByUser = Number(event.data.val());
    let usersThatBoughtTicket = event.data.key();
})

I want to get the users UID, which is a string, and bought tickets (which is 1, the value of the key). How can I those values? This is my code in Swift:

 let values = [usersUID! : 1] // 1 can be changed
            let refLottery: FIRDatabaseReference = FIRDatabase.database().reference().child("lottery").child("ticketsneedstobeprocessed")
            let randomDirectory = refLottery.childByAutoId()
            randomDirectory.updateChildValues(values)

The console logs NaN when I try to log ticketsBoughtByUser. I have no idea what I am doing wrong... Thank you.



via J. Doe

No comments:

Post a Comment