函数以状态完成:'ok',但控制台日志显示函数返回未定义、预期的 Promise 或值
Posted
技术标签:
【中文标题】函数以状态完成:\'ok\',但控制台日志显示函数返回未定义、预期的 Promise 或值【英文标题】:Function finished with status: 'ok' but console logs shows Function returned undefined, expected Promise or value函数以状态完成:'ok',但控制台日志显示函数返回未定义、预期的 Promise 或值 【发布时间】:2022-01-09 01:50:22 【问题描述】:我正在尝试使用 firebase 云函数发送通知,但是当我更新字段时,它显示函数首先返回未定义、预期的 Promise 或值,然后函数执行耗时 364 毫秒,完成状态为:'ok'。
我的代码
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.updateOrderStatus = functions.firestore
.document("Users/userId/Info_Notifications/notificationId")
.onUpdate((change, context) =>
const newValue = change.after.data();
const notificationFor = newValue.orderFrom;
let deviceID;
const db = admin.firestore();
db.collection("Tokens").doc(notificationFor).get()
.then((doc) =>
const user = doc.data();
deviceID = user.id;
console.log("Device id", deviceID);
const payload =
token: deviceID,
notification:
title: "Test Notification",
body: "message",
,
data:
priority: "high",
timeToLive: "60 * 60* 24",
,
;
console.log("Device ID Out", deviceID);
admin.messaging().sendToDevice(payload)
.then((response) =>
return true;
)
.catch((error) =>
return console.log("Error Sending Notifications", error);
);
return null;
).catch((error)=>
return console.log("Error in fetching data", error);
);
);
【问题讨论】:
【参考方案1】:您在 db.collection("Tokens").doc(notificationFor).get() 行中缺少返回
你也应该返回 null 并确保你传递了一个承诺或价值
const functions = require("firebase-functions");
const admin = require("firebase-admin");
admin.initializeApp();
exports.updateOrderStatus = functions.firestore
.document("Users/userId/Info_Notifications/notificationId")
.onUpdate((change, context) =>
const newValue = change.after.data();
const notificationFor = newValue.orderFrom;
let deviceID;
const db = admin.firestore();
return db.collection("Tokens").doc(notificationFor).get()
.then((doc) =>
const user = doc.data();
deviceID = user.id;
console.log("Device id", deviceID);
const payload =
token: deviceID,
notification:
title: "Test Notification",
body: "message",
,
data:
priority: "high",
timeToLive: "60 * 60* 24",
,
;
console.log("Device ID Out", deviceID);
admin.messaging().sendToDevice(payload)
.then((response) =>
return true;
)
.catch((error) =>
console.log("Error Sending Notifications", error);
return false;
);
).catch((error)=>
console.log("Error in fetching data", error);
return false;
);
);
【讨论】:
message: '提供给 sendToDevice() 的注册令牌必须是非空字符串或非空数组。'但在日志语句中它打印 DeviceId 我认为sendToDevice方法有2个参数,第一个是token,第二个是payload,以admin.messaging().sendToDevice(deviceID,payload)的形式发送数据以上是关于函数以状态完成:'ok',但控制台日志显示函数返回未定义、预期的 Promise 或值的主要内容,如果未能解决你的问题,请参考以下文章