0

New to Cloud Functions and trying to understand my error here from the log. It says cannot read property 'uid' of undefined. I am trying to match users together. onCreate will call matching function to check if a user exists under live-Channels and if so will set channel value under both users in live-Users to uid+uid2. Does the log also say which line the error is from? Confused where it shows that.

const functions = require('firebase-functions');

//every time user added to liveLooking node
exports.command = functions.database
        .ref('/liveLooking/{uid}')
        .onCreate(event => {
    const uid = event.params.uid
    console.log(`${uid} this is the uid`)
        
    const root = event.data.adminRef.root
    //match with another user
    let pr_cmd = match(root, uid)

    const pr_remove = event.data.adminRef.remove()
    return Promise.all([pr_cmd, pr_remove])
})

function match(root, uid) {
    let m1uid, m2uid
    return root.child('liveChannels').transaction((data) => {
    //if no existing channels then add user to liveChannels
    if (data === null) {
        console.log(`${uid} waiting for match`)
        return { uid: uid }
    }
    else {
        m1uid = data.uid
        m2uid = uid
        if (m1uid === m2uid) {
            console.log(`$m1uid} tried to match with self!`)
            return
        }
        //match user with liveChannel user
        else {
            console.log(`matched ${m1uid} with ${m2uid}`)
            return {}
        }
    }
},
(error, committed, snapshot) => {
    if (error) {
        throw error
    }
    else {
         return {
            committed: committed,
            snapshot: snapshot
        }
    }
},
false)
    .then(result => {
        // Add channels for each user matched
        const channel_id = m1uid+m2uid
        console.log(`starting channel ${channel_id} with m1uid: ${m1uid}, m2uid: ${m2uid}`)
        const m_state1 = root.child(`liveUsers/${m1uid}`).set({
            channel: channel_id
        })
        const m_state2 = root.child(`liveUsers/${m2uid}`).set({
            channel: channel_id
        })
        return Promise.all([m_state1, m_state2])
    })
}

cloud functions log

1 Answer 1

1

You are referring to a very old version of the Cloud Functions API. Whatever site or tutorial you might be looking it, it's showing examples that are no longer relevant.

In modern Cloud Functions for Firebase, Realtime Database onCreate triggers receive two parameters, a DataSnapshot, and a Context. It no longer receives an "event" as the only parameter. You're going to have to port the code you're using now to the new way of doing things. I strongly suggest reviewing the product documentation for modern examples.

If you want to get the wildcard parameters as you are trying with the code const uid = event.params.uid, you will have to use the second context parameter as illustrated in the docs. To access the data from snapshot, use the first parameter.

Sign up to request clarification or add additional context in comments.

2 Comments

Ok. Is there anything else that is no longer being used?
That's the main thing. The APIs have evolved over the years. The documentation always stays up to date, so always start there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.