【发布时间】:2020-12-23 08:41:22
【问题描述】:
云功能和承诺的新手。我尝试在不同的位置添加承诺,但仍然在日志中收到消息。首先,我不确定我应该在哪里添加承诺,其次我是否应该什么都不返回。调用 match 后我不需要执行另一个函数(如果条件满足则创建通道)。虽然触发 onCreate 会有多个用户,所以我想确保一次执行一个用户。
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
})
}
编辑 1 - 我尝试将交易更改为使用 await,因此它只会在交易后更改 userLives 节点。收到这两个警告。 1) 预期在异步函数“匹配”结束时返回一个值。 2) 在 ***return 箭头函数需要一个返回值。如果使用与 self 匹配,我不会尝试更改 LiveChannels 下的任何内容。不知道如何解决该警告。 3) 仍然在日志中返回未定义、预期的承诺或值的函数 - 我认为是命令函数。
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);
}
}
【问题讨论】:
-
哪个函数应该返回一个 Promise 但返回 undefined?
-
不确定我是否需要对 onCreate one 和 match 都做出承诺?
标签: javascript firebase firebase-realtime-database promise google-cloud-functions