【发布时间】:2018-07-26 08:02:11
【问题描述】:
我在这里为我的项目寻找使用 Firebase Firestore 和 Cloud Functions 的解决方案。
问题制造者
直到昨天早上,一切正常。下午我通过控制台读到“npm”和 Node 可以更新。所以我做到了,问题开始了。
问题:
首先我遇到了著名的“$RESOURCE_DIR”问题,解决了用“functions”替换该字符串的问题。之后,我可以在 Firebase (无需编辑) 上编译和发布函数,它们可以正常工作,但有一个(大)问题: 在 Firebase 平台上的控制台日志中,我总是阅读 foreach 函数,它以 timeout 状态关闭,即使它完成正确。
在控制台中我可以阅读消息
消息发送成功......
我在设备上收到通知,但在 60 秒后我收到了
函数执行耗时 60002 毫秒,完成状态为:'timeout'
嗯...这是我无法理解如何解决问题的地方。任何人都可以帮助我吗?
这是产生错误的简单函数之一:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const db = admin.firestore();
exports.notifyProfileApproved = functions.firestore.document('/users/{user_id}')
.onUpdate((change , context) => {
const approvedBefore = change.before.data().approved;
const approvedAfter = change.after.data().approved;
if(approvedBefore === false && approvedAfter === true){
var registrationToken = change.after.data().registrationToken;
var message = {
notification: {
title: 'Approved profile',
body: 'Your profile has been approved!'
},
token : registrationToken
};
return admin.messaging().send(message)
.then((response) =>{
return console.log('Successfully sent message: ', response);
})
.catch((error) =>{
return console.log('Error sending message: ', error);
});)
}else{
return console.log('Profile edited but not approved');
}
});
这是我的系统信息:
npm:4.2.0
节点:7.10.0
火力基地:4.0.0
更新
2018 年 7 月 17 日,我收到一封 Firebase 电子邮件,告诉我我的项目已自动更新为 Blaze 计划,因为 Google Cloud Platform 发生了变化,但我从未输入过帐单方法。 没有做任何明显的事情(只重启PC,重新部署并切换到免费Spark计划),现在执行时间从50ms到350ms,所以没关系!
即使功能正常工作,控制台中也会出现新错误。错误是:
函数返回未定义的、预期的 Promise 或值
功能代码同上。为了解决这个问题,每次我没有承诺时,我都必须返回一个值。所以,我每次都改变了
return console.log('', response);
与
console.log('', response);
return 0;
现在一切正常!
【问题讨论】:
-
你有什么问题?听起来你解决了所有问题。
-
是的,现在一切正常.. 但我不知道如何将主题设置为 [已解决] 状态.. 刚才,我在 StackOverflow 上看到这条评论解释了实际发生的情况:@ 987654321@
-
您可以回答自己的问题并接受它作为正确答案。
-
谢谢@DougStevenson
标签: node.js firebase firebase-cloud-messaging google-cloud-functions