gpt4 book ai didi

javascript - Firebase 函数 :Function returned undefined, 预期的 promise 或值(value)

转载 作者:行者123 更新时间:2023-11-29 20:52:38 25 4
gpt4 key购买 nike

我已经编写了一个 firebase 函数来在我的 android 应用程序中发生类似事件时发送通知。通知功能在大多数情况下运行良好,但有时不起作用。

我总是收到这个错误(无论它是否工作):

Function returned undefined, expected Promise or value

这是我点赞功能的代码:

exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}')
.onWrite(event => {


if (event.data.exists())
{

const message = event.data.val();
const userUid = event.params.user_id;

const ownerUid = message.owner_id;
console.log("Owner id", ownerUid);
const userPic = message.thumb_image;
const userName = message.name;
const post_key = event.params.post_id;
const timestamp = admin.database.ServerValue.TIMESTAMP;

if(ownerUid == userUid){return null;}

const Promise1= admin.database().ref(`/notifs/${ownerUid}`).push({
thumb_image: userPic,
name: userName,
user_id: userUid,
post_id: post_key,
text: "liked your post",
type: "Like",
read: "false",
time: timestamp
});

const Promise2=admin.database().ref(`/Users/${ownerUid}/device_token`).once('value');

const Promise3= Promise2.then(function(snapshot) {
const getrealDeviceTokensPromise = snapshot.val();
console.log("Device Token", getrealDeviceTokensPromise);

// Notification details.
const payload = {
notification: {
title: 'Appname',
body: userName + ' has liked your post.',
icon: "default",
sound: "default",
click_action: "OPEN_ACTIVITY_1"
}
};

const Promise4= admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
return Promise.all([Promise1,Promise3,Promise4]);
})
.catch(function (error) {
console.log("Error sending message:", error);
return null;
});

}, function(error) {
// The Promise was rejected.
console.error(error);
return null;
});

}

else
{
return null;
}


});

我不明白我哪里错了。请帮忙!

最佳答案

exports.sendactorLikeNotification = functions.database.ref('/Likes/{post_id}/{user_id}').onWrite(event => {
if (event.data.exists()) {

const promises=[];



const message = event.data.val();
const userUid = event.params.user_id;

const ownerUid = message.owner_id;
console.log("Owner id", ownerUid);
const userPic = message.thumb_image;
const userName = message.name;
const post_key = event.params.post_id;
const timestamp = admin.database.ServerValue.TIMESTAMP;

if(ownerUid == userUid){return null;}

const a1=admin.database().ref(`/notifs/${ownerUid}`).push({
thumb_image: userPic,
name: userName,
user_id: userUid,
post_id: post_key,
text: "liked your post",
type: "Like",
read: "false",
time: timestamp
});

promises.push(a1);




const a2= admin.database().ref(`/Users/${ownerUid}/device_token`).once('value').then(function(snapshot) {






const getrealDeviceTokensPromise = snapshot.val();

console.log("Device Token", getrealDeviceTokensPromise);

// Notification details.
const payload = {
notification: {
title: 'Appname',
body: userName + ' has liked your post.',
icon: "default",
sound: "default",
click_action: "OPEN_ACTIVITY_1"
}
};

const a3=admin.messaging().sendToDevice(getrealDeviceTokensPromise, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});


promises.push(a3);


}, function(error) {

console.error(error);
});
promises.push(a1);
return Promise.all(promises);

}

else
{
return null;
}

});

这段代码解决了我的问题!

关于javascript - Firebase 函数 :Function returned undefined, 预期的 promise 或值(value),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51051667/

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