gpt4 book ai didi

javascript - Firebase 函数 - admin.messaging().sendToTopic 执行但从未到达 `then` block

转载 作者:行者123 更新时间:2023-12-03 01:05:54 33 4
gpt4 key购买 nike

由于某种原因then block admin函数似乎没有执行 - 我没有看到任何 console.log firebase console 中的消息:

这是我的全部 firebase functions代码:

// The Cloud Functions for Firebase SDK to create Cloud Functions and setup triggers.
const functions = require('firebase-functions');
const firebaseHelper = require('firebase-functions-helper');
const serviceAccount = require('./serviceAccountKey.json');
var toPlainObject = require('lodash.toplainobject');

// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
//const firestore = require('firebase-firestore');

//firebaseHelper.firebase.initializeApp(serviceAccount, 'https://snag-b2b2d.firebaseio.com');

//if (!admin.apps.length) {
admin.initializeApp();
admin.firestore().settings({timestampsInSnapshots: true});
var db = admin.firestore();

function renameObjectKey(oldObj, oldName, newName) {
const newObj = {};

console.log("in renameObjectKey");

Object.keys(oldObj).forEach(key => {
const value = oldObj[key];

if (key === oldName) {
newObj[newName] = value;
} else {
newObj[key] = value;
}
});

return newObj;
}

class ParamsObject {
constructor(value, tempId) {
this.data = {message: value, tempId: tempId};
}
}

exports.sendMessageNotification = functions.firestore.document('messages/{messageId}').onWrite((change, context) => {

// Get an object representing the document
// e.g. {'name': 'Marie', 'age': 66}
const newValue = change.after.data();

// ...or the previous value before this update
const previousValue = change.before.data();

console.log("newValue:", newValue);

console.log("messageIdChange:", context.params.messageId);
//console.log("prevValue:", previousValue);

// access a particular field as you would any JS property
//const name = newValue.name;

var topic = 'all';

let params = toPlainObject(new ParamsObject(newValue[context.params.messageId].message, newValue[context.params.messageId].timestamp));
//params.data = toPlainObject(new WebObject());

/*var payload = {
data: {
message: newValue.data.message
}
};*/

admin.messaging().sendToTopic(topic, params).then((response) => {
console.log("Successfully sent message:", response);
//console.log("Message ID:", response.messageId);

var newObj = renameObjectKey(newValue, newValue[context.params.messageId].timestamp, response.messageId);

console.log("newObj:", newObj);

firebaseHelper.firestore.updateDocument(db, 'messages', newValue[context.params.messageId].timestamp, newObj);
}).catch((error) => {
console.log("Error sending message:", error);
});

return null;
});

未执行的代码位于 messaging 内函数调用 - 我收到消息,所以它至少已经达到了那么远,但它似乎没有进入 then block :

admin.messaging().sendToTopic(topic, params).then((response) => {

//*******DOESNT SEEM TO MAKE IT HERE*******

console.log("Successfully sent message:", response);


var newObj = renameObjectKey(newValue, newValue[context.params.messageId].timestamp, response.messageId);

console.log("newObj:", newObj);

firebaseHelper.firestore.updateDocument(db, 'messages', newValue[context.params.messageId].timestamp, newObj);
}).catch((error) => {
console.log("Error sending message:", error);
});

这就是我在 firebase functions 中看到的全部内容日志:

2:43:14.793 PM
sendMessageNotification
Function execution took 682 ms, finished with status: 'ok'
2:43:14.508 PM
sendMessageNotification
messageIdChange: 1537382591952
2:43:14.497 PM
sendMessageNotification
newValue: { '1537382591952': { message: 'The relay seems to be malfunctioning.', name: 'eamon', timestamp: '1537382591952' } }
2:43:14.112 PM
sendMessageNotification
Function execution started

我还应该看到以下输出:

console.log("Successfully sent message:", response);

至少...

发生了什么?

最佳答案

您需要从函数返回一个 promise ,该 promise 会在所有异步工作完成时解析。如果您不这样做,Cloud Functions 将终止您的函数,可能会在工作完成之前终止。

就您而言,您应该在 admin.messaging()... 前面放置一个 return 关键字,而不是返回 null。

read the documentation欲了解更多信息,和 watch my video series on dealing with promises in Cloud Functions .

关于javascript - Firebase 函数 - admin.messaging().sendToTopic 执行但从未到达 `then` block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52412488/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com