Javascript 如何使用Cloud Function.onWrite(Firebase)从其他数据库节点获取变量
如果Firebase节点设置为true,我将尝试设置一些要发送到函数中的变量。我正试图使用Javascript 如何使用Cloud Function.onWrite(Firebase)从其他数据库节点获取变量,javascript,node.js,firebase,firebase-realtime-database,google-cloud-functions,Javascript,Node.js,Firebase,Firebase Realtime Database,Google Cloud Functions,如果Firebase节点设置为true,我将尝试设置一些要发送到函数中的变量。我正试图使用.parent和.val()函数来根据以下文档设置客户id: 我希望snap.ref.parent引用/sources和.child('customer_id').val()从customer_id键访问值 但是,当我尝试运行此函数时,出现以下错误: TypeError: snap.ref.parent.child(...).val is not a function at exports.linkCard
.parent
和.val()
函数来根据以下文档设置客户id
:
我希望snap.ref.parent
引用/sources
和.child('customer_id').val()
从customer_id
键访问值
但是,当我尝试运行此函数时,出现以下错误:
TypeError: snap.ref.parent.child(...).val is not a function
at exports.linkCardToSquareAccount.functions.database.ref.onWrite.event (/user_code/index.js:79:56)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
at process._tickDomainCallback (internal/process/next_tick.js:129:7)
如何引用原始onWrite位置范围之外的节点?您不能仅对数据库引用调用
.val()
,然后期望在该位置获取数据。您需要添加一个值侦听器以获取新数据
幸运的是,云功能内部完全支持这一点:
exports.newCloudFunction = functions.database.ref('/user/{userId}/sources/saveSource').onWrite(event => {
// Retrieve true/false value to verify whether card should be kept on file
const saveSource = event.data.val();
if (saveSource) {
const customerIdRef = event.data.adminRef.parent.child('customer_id')
// attach a 'once' value listener to get the data at this location only once
// this returns a promise, so we know the function won't terminate before we have retrieved the customer_id
return customerIdRef.once('value').then(snap => {
const customer_id = snap.val();
console.log(customer_id);
// use customer_id here
});
}
});
您可以了解更多信息。您不能只对数据库引用调用
.val()
,然后期望在该位置获取数据。您需要添加一个值侦听器以获取新数据
幸运的是,云功能内部完全支持这一点:
exports.newCloudFunction = functions.database.ref('/user/{userId}/sources/saveSource').onWrite(event => {
// Retrieve true/false value to verify whether card should be kept on file
const saveSource = event.data.val();
if (saveSource) {
const customerIdRef = event.data.adminRef.parent.child('customer_id')
// attach a 'once' value listener to get the data at this location only once
// this returns a promise, so we know the function won't terminate before we have retrieved the customer_id
return customerIdRef.once('value').then(snap => {
const customer_id = snap.val();
console.log(customer_id);
// use customer_id here
});
}
});
您可以了解更多。太棒了。谢谢你,布拉德利!!链接已弃用。参考这个:太棒了。谢谢你,布拉德利!!链接已弃用。请参阅: