当我尝试将数组作为云函数的参数时。在我的代码中,数组显示正确,但在cloud函数的日志中,它显示为未定义。
数组结果:["user1"、"user2"]
云功能:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.chatNotifications = functions.https.onCall((data, context) => {
const title = data.title;
const payloadMsg = data.message;
const memberList = data.membersList;
functions.logger.log("Member List:", memberList);
for (let i = 0; i < memberList.count; i++) {
const memberName = memberList[i];
const message = {
notification: {
title: title,
body: payloadMsg,
},
topic: memberName,
apns: {
payload: {
aps: {
badge: "1",
},
},
},
};
admin.messaging().send(message)
.then((response) => {
// Response is a message ID string.
console.log("Successfully sent message:", response);
})
.catch((error) => {
console.log("Error sending message:", error);
});
}
});
代码:
functions.httpsCallable("chatNotifications").call(["title": chatName, "message": name + " has been added to the group", "memberList": memberList]) { (result, error) in
if let error = error as NSError? {
if error.domain == FunctionsErrorDomain {
let message = error.localizedDescription
print(message)
}
}
if let text = result?.data as? String {
print("Result: ", text)
}
}
我可以看到,在您的代码中,您发送的是:
"memberList": memberList
但在云功能中,你有这样的:
data.membersList
数据的名称不同,也使用
memberList.length
而不是
memberList.count