Firebase云功能始终在日志中超时 [英] Firebase cloud function always timeout in logs

查看:73
本文介绍了Firebase云功能始终在日志中超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过以下链接使用代码: https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#9

I am using code from this link: https://codelabs.developers.google.com/codelabs/firebase-cloud-functions/#9

并且我总是在日志中超时以获取云功能,这是一个代码:

and always I get timeout in logs for cloud function, here is a code:

exports.sendPatrola = functions.database.ref('/test/tmp')
.onUpdate((change, context) => {

const original = change.after.val();

var payload = {
data: {
id: String(original.id),
x: String(original.x),
y: String(original.y),
dat: String(original.dat)
}
};

 var options = {
  priority: 'high',
 contentAvailable: true, 
 timeToLive: 60 * 1
};



let tokens = []; // All Device tokens to send a notification to.
// Get the list of device tokens.
return admin.database().ref('keys').once('value').then((allTokens) => {
if (allTokens.val()) {
  // Listing all tokens.
  tokens = Object.keys(allTokens.val());

  // Send notifications to all tokens.
  return admin.messaging().sendToDevice(tokens, payload, options);
}
return {results: []};
}).then((response) => {
return cleanupTokens(response, tokens);
}).then(() => {
console.log('Notifications have been sent and tokens cleaned up.');
return null;
});
 }

错误消息是:函数执行耗时60002 ms,状态为:超时"

Error message is: Function execution took 60002 ms, finished with status: 'timeout'

这是怎么了?

推荐答案

请尝试这种方式,函数cleanupTokens应该返回一个promise:

Please try this way, the function cleanupTokens should return a promise:

exports.sendPatrola = functions.database.ref('/test/tmp').onUpdate((change, context) => {
  const original = change.after.val();
  var payload = {
    data: {
      id: String(original.id),
      x: String(original.x),
      y: String(original.y),
      dat: String(original.dat)
    }
  };
  var options = {
    priority: 'high',
    contentAvailable: true,
    timeToLive: 60 * 1
  };
  let tokens = [];
  return admin.database().ref('keys').once('value').then((allTokens) => {
    if (allTokens.val()) {
      tokens = Object.keys(allTokens.val());
      return admin.messaging().sendToDevice(tokens, payload, options);
    } else {
      const result = [];
      return result;
    }
  }).then((response) => {
    return cleanupTokens(response, tokens);
  }).then(() => {
    console.log('Notifications have been sent and tokens cleaned up.');
    return null;
  });
});

这篇关于Firebase云功能始终在日志中超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆