我有一个Firebase数据库,我希望创建一个云函数,该函数在将子节点添加到父节点时触发,该函数应使用在父节点中添加的子节点的参数来调用url。
将要调用的URL是Google App Engine中托管的NodeJS Express应用。
如果可能的话,我该怎么办?
答案 0 :(得分:1)
您可以使用node.js request库来实现。
由于在Cloud Function中,执行异步任务时必须返回Promise,因此需要使用接口包装程序来进行请求,例如request-promise。
您可以按照以下方式进行操作:
'NUMERIC'
如果要将参数传递给所调用的HTTP(S)服务/端点,则可以通过请求的正文来完成,例如:
.....
var rp = require('request-promise');
.....
exports.yourCloudFucntion = functions.database.ref('/parent/{childId}')
.onCreate((snapshot, context) => {
// Grab the current value of what was written to the Realtime Database.
const createdData = snapshot.val();
var options = {
url: 'https://.......',
method: 'POST',
body: ....
json: true // Automatically stringifies the body to JSON
};
return rp(options);
});
或通过一些查询字符串键值对,例如:
.....
const createdData = snapshot.val();
var options = {
url: 'https://.......',
method: 'POST',
body: {
some: createdData.someFieldName
},
json: true // Automatically stringifies the body to JSON
};
.....