Javascript 云函数无法读取未定义的属性
刚接触云函数,并试图从日志中了解我的错误。它表示无法读取未定义的属性“uid”。我正在尝试将用户匹配在一起。onCreate将调用匹配函数检查live channel下是否存在用户,如果存在,则将live users中两个用户下的频道值设置为uid+uid2。日志是否也说明错误来自哪一行?我不知道它显示了什么Javascript 云函数无法读取未定义的属性,javascript,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Firebase,Firebase Realtime Database,Google Cloud Functions,刚接触云函数,并试图从日志中了解我的错误。它表示无法读取未定义的属性“uid”。我正在尝试将用户匹配在一起。onCreate将调用匹配函数检查live channel下是否存在用户,如果存在,则将live users中两个用户下的频道值设置为uid+uid2。日志是否也说明错误来自哪一行?我不知道它显示了什么 const functions = require('firebase-functions'); //every time user added to liveLooking node
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])
})
}
您指的是非常旧版本的云函数API。无论你在看什么网站或教程,它都会展示一些不再相关的例子 在Firebase的现代云函数中,实时数据库onCreate触发器接收两个参数,一个DataSnapshot和一个上下文它不再接收“事件”作为唯一参数。您必须将现在使用的代码移植到新的操作方式。我强烈建议大家复习一下现代的例子
如果要在尝试使用代码
const uid=event.params.uid
时获取通配符参数,则必须使用第二个上下文参数,如文档中所示。要从快照访问数据,请使用第一个参数。Ok。还有什么东西不再使用了吗?这是最重要的。这些API经过多年的发展。文档始终保持最新,因此始终从那里开始。