New to cloud functions and promises. I've tried adding promises in different locations but still getting the message in logs. First, I'm not sure where I should be adding the promise and second whether I should be returning nothing. I don't need to execute another function after I call match (and create channel if condition met). There will be multiple users though triggering onCreate so I want to make sure things are executed one user at a time.
const functions = require('firebase-functions');
const admin = require('firebase-admin')
admin.initializeApp();
//every time user added to liveLooking node
exports.command = functions.database
.ref('liveLooking/{uid}')
.onCreate((snap, context) => {
const uid = context.params.uid
match(uid)
})
function match(uid) {
let m1uid, m2uid
admin.database().ref('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!`)
//match user with liveChannel user
} else {
console.log(`matched ${m1uid} with ${m2uid}`)
createChannel(m1uid, m2uid)
return null
}
}
},
(error, committed, snapshot) => {
if (error) {
throw error
}
else {
return {
committed: committed,
snapshot: snapshot
}
}
},
false)
}
function createChannel(uid1, uid2) {
// Add channels for each user matched
const channel_id = uid1+uid2
console.log(`starting channel ${channel_id} with uid1: ${uid1}, uid2: ${uid2}`)
const m_state1 = admin.database().ref(`liveUsers/${uid1}`).set({
channel: channel_id
})
const m_state2 = admin.database().ref(`liveUsers/${uid2}`).set({
channel: channel_id
})
}
Edit 1 - I tried changing the transaction to use await so it will change userLives node only after the transaction. Getting these two warnings. 1) Expected to return a value at the end of async function 'match'. 2) At ***return Arrow function expected a return value. I'm not trying to change anything under LiveChannels if use matches with self. Not sure how to fix that warning. 3) Still getting function return undefined, expected promise or value in the logs - I think for command function.
async function match(uid) {
let m1uid, m2uid;
let createChannel = false
try {
const transactionResult = await admin
.database()
.ref("liveChannels")
.transaction(
(data) => {
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***
} else {
console.log(`matched ${m1uid} with ${m2uid}`)
createChannel = true
return {}
}
}
},
(error, committed, snapshot) => {
if (error) {
throw error
}
else {
return {
committed: committed,
snapshot: snapshot
}
}
},
false
);
if (transactionResult) {
if (createChannel) {
const channel_id = m1uid + m2uid
console.log(`starting channel ${channel_id} with uid1: ${m1uid}, uid2: ${m2uid}`)
const m_state1 = admin.database().ref(`liveUsers/${m1uid}`).set({
channel: channel_id
})
const m_state2 = admin.database().ref(`liveUsers/${m2uid}`).set({
channel: channel_id
})
return Promise.all([m_state1, m_state2])
}
}
} catch (err) {
throw new Error(err);
}
}