Tag: google cloud functions

Firebase的Cloud Functions中将对象从一个节点复制到另一个节点

我正在使用Firebase的云端函数,而且我似乎是一个非常基本的操作。 如果有人添加post,他会写入/posts/ 。 我想要使​​用与初始文章中使用的相同的密钥将该post的一部分保存在不同的节点(称为public-posts或private-postspost)下。 我的代码看起来像这样 const functions = require('firebase-functions'); exports.copyPost = functions.database .ref('/posts/{pushId}') .onWrite(event => { const post = event.data.val(); const smallPost = (({ name, descr }) => ({ name, descr }))(post); if (post.isPublic) { return functions.database.ref('/public-posts/' + event.params.pushId) .set(smallPost); } else { return functions.database.ref('/private-posts/' + event.params.pushId) .set(smallPost); } }) 我得到的错误信息是: functions.database.ref(…).set不是一个函数 。 我究竟做错了什么?

如何从Google Cloud Functions发送HTTP请求(nodeJS)

这可能是一个简单的问题,但我是云新function/节点编程,还没有find正确的文件。 如何编写Google云端function,接收HTTP请求,然后向另一个端点发送HTTP请求? 例如,我可以将HTTP触发器发送到我的云端function( https://us-central1-plugin-check-xxxx.cloudfunctions.net/HelloWorldTest )。 后来在这个项目中,我会弄清楚如何实现延迟。 但是接下来我想用新的HTTP请求来响应不同的端点( https://maker.ifttt.com/trigger/arrive/with/key/xxxx )。 我怎么做? exports.helloWorld = function helloWorld(req, res) { // Example input: {"message": "Hello!"} if (req.body.message === undefined) { // This is an error case, as "message" is required. res.status(400).send('No message defined!'); } else { // Everything is okay. console.log(req.body.message); res.status(200).send('Success: ' + req.body.message); // ??? send a […]

我可以使用Firebase云端function隐藏JavaScript代码吗?

我有一个JavaScript网页,发送MQTT数据到运行Mosquitto的服务器。 我想隐藏用户的MQTT服务器名称,用户名和密码。 我知道用普通的JavaScript是不可能的,因为源代码在控制台中是可见的。 Firebase云function可以做到吗? 如果是的话,怎么样?

如何使用Firebase云function存储制作文件夹

我怎样才能使文件夹“职位”? 存储桶 – > gs://app.appspot.com/posts 码 exports.generateThumbnail = functions.storage.object() .onChange(event => { const object = event.data const filePath = object.name const fileName = filePath.split('/').pop() const fileBucket = object.bucket; const bucket = gcs.bucket(fileBucket) const tempFilePath = `/posts/${fileName}` return bucket.file(filePath).download({ destination: tempFilePath }) .then(() => { console.log('image downloaded locally to', tempFilePath) return spawn('convert', [tempFilePath, '-thumbnail', '400×400>', tempFilePath]) […]

BigQuery读取ECONNRESET

将Firebase云端函数与Google BigQuery结合使用时。 有时这个函数被触发时会随机抛出一个错误。 这是我们的错误日志: Error: read ECONNRESET at exports._errnoException (util.js:1026:11) at TLSWrap.onread (net.js:569:26) 这里是我的同事的代码。 const bigQuery = require('@google-cloud/bigquery'); const admin = require('firebase-admin'); const database = admin.database(); exports.updateAllPlaceStatistics = (request, response) => { const secret = request.query['secret']; if (secret !== 'secret') { return response.json({message: 'Request failed!'}); } const big = bigQuery(); return big.query({ query: [ 'SELECT […]

如何使用Cloud Functions for Firebase向特定用户发送推送通知

我使用Firebase作为我的Android应用程序的后端,并且对于使用Firebase的云端函数非常新,我想知道如何在发生事件时发送特定用户的推送通知。 例如,如果在数据库的adminName节点上发生写入时,如何在下面的代码中使用uId向用户发送推送通知: exports.sendNotification = functions.database.ref('/users/{uId}/groups/{adminName}') .onWrite(event => { // Grab the current value of what was written to the Realtime Database. var eventSnapshot = event.data; var str1 = "Author is "; var str = str1.concat(eventSnapshot.child("author").val()); console.log(str); var topic = "android"; var payload = { data: { title: eventSnapshot.child("title").val(), author: eventSnapshot.child("author").val() } }; // Send a […]

推送通知会在Google云端函数中返回ECONNRESET

我在Firebase Cloud Functions中具有检索用户的设备组ID的function,以发送推送通知,并在发送推送通知之后进行。 如果这个函数只被调用一次,这个效果很好,但是如果我有一个用户数组,我也想发送一个推送通知,sendPushNotification函数返回错误: FAILED err= { RequestError: Error: read ECONNRESET at new RequestError (/user_code/node_modules/request-promise/node_modules/request-promise-core/lib/errors.js:14:15)每次尝试发送推送 从我所了解的ECONNRESET意味着连接在完成操作之前一端被closures了,可以有一些帮助/解释我为什么这样做: 这里是我的代码: function sendFollowNotification(snapshot) { const notificationMsg = getFollowNotificationMsg() //returns a string snapshot.forEach(function(singleUser, index) { const userId = singleUser.key; const userObject = singleUser.val(); console.log("will get device group") if (index + 1 == snapshot.numChildren()) { return getDeviceGroupNotificationKey(userId, "Discover new artists", notificationMsg, […]

如何使用Node.js控制Cloud PubSub中的确认

基本上我已经创build了一个云function(写一个Node.js代码),它将触发云pubsub主题的消息,并将数据加载到Bigquery表。 主题中的消息在被云function读取后被删除。 我了解,订户内部发送确认和结果,消息从主题中删除。 我想控制发送给发布者的确认。 怎么能实现,没有find任何文件。

Google云端函数 – Nodejs错误,永远加载

const request = require('request'); exports.helloWorld = function helloWorld(req, res) { var headers = { 'scheme': 'https', 'authorization': 'Token 123', 'user-agent': 'mobile' }; var options = { url: 'https://url', headers: headers }; function callback(error, response, body) { if (!error && response.statusCode == 200) { console.log(body); res.status(200).send(response.body); } } request(options, callback); }; 我可以提交一个没有问题的请求,但一旦我开始做一个请求头和选项,我不能让它在Google云端函数上正常工作。 请教我的错误 触发器types:HTTP触发器

从Firebasefunction(节点)上传时,Firebase存储文件types未设置

通过Firebase函数(节点)将转换后的图像(.jpg)上传到谷歌存储时,我在元数据选项中设置了contentType。 return bucket.file(filePath).download({destination: tempFilePath}) .then(() => { console.log('Image downloaded locally to', tempFilePath); // Generate a thumbnail using ImageMagick. return spawn('convert', [tempFilePath, '-thumbnail', '200×200>', tempFilePath]) }) .then(() => { console.log('Thumbnail created at', tempFilePath); // Add a 'thumb_' prefix to thumbnails file name. That's where we'll upload the thumbnail. const thumbFilePath = filePath.replace(/(\/)?([^\/]*)$/, `$1thumb_$2`); // Uploading the […]