Firebase cloud function always timeout in logs

时间秒杀一切 提交于 2020-01-06 05:45:10

问题


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;
});
 }

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

What is wrong here?


回答1:


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;
  });
});


来源:https://stackoverflow.com/questions/51525260/firebase-cloud-function-always-timeout-in-logs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!