问题
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