【问题标题】:Firebase Function throws an error: returned undefined, expected Promise or value. How do I resolve thisFirebase 函数抛出错误:返回未定义、预期的 Promise 或值。我该如何解决这个问题
【发布时间】:2020-08-10 00:00:44
【问题描述】:

这是我的 .ts 文件,我使用 then 方法并使用 catch 块来处理最终返回语句中的错误。这是一个 Firestore 触发的后台功能,基本上我在创建文档时做 3 件事 1. 将数据复制到 Firestore 中的另一个文档 2. 发送 FCM 通知 3. 创建一个名为“notificationObject”的 JSON 对象并将其添加到 Firestore 的另一个文档中

import * as functions from 'firebase-functions'
const admin = require('firebase-admin')

//When client followes a user, a firestore .onCreate() background function is triggered to
//1.add follower to the followee's followers sub collection
//2.an FCM notification to sent to the users
//3.A Notification doc is added to Notification Sub Collection
export const addTheNewFollower = functions.region('asia-east2').firestore.document
  ('Users/{followerUserId}/following/{followeeUserId}').onCreate((data, context) => {

  //get follower and followee Uids for identification
  const followeeUid = context.params.followeeUserId
  //for identification and notification payload data (Intent Extras for client)
  const followerUid = context.params.followerUserId

  //Get Follower user details that needs to be duplicated to the Followee's following Sub Coll
  //And also added to the notification Payload data
  admin.firestore().collection('Users').doc(followerUid).get().then((doc:{ exists: any; data: () =>         
any }) => {

//Extracting this separately as this need not be copied to the Followers sub-collection
const followerImageUrl = doc.data().DOWNLOAD_URL

//This data will be copied to the followers sub collection
const followerData = {
  name:  doc.data().name,
  uid: followerUid,
  userName: doc.data().userName,
}


  //get the notification token of the followee to identify & send notification to his device
  admin.firestore().collection('Users').doc(followeeUid).collection('notificationToken')
    .doc('theNotificationToken').get().then((notificationTokenDoc:{ exists: any; data: () => any })     
=> {

  const followeeNotificationToken = notificationTokenDoc.data().notificationToken

  //Create the Notification Payload content
  const notificationPayload = {
    notification: {
  title: 'You have a new follower!',
  body: `${followerData.userName}`,
  clickAction: ".People.PersonProfileActivity",
  image: `${followerImageUrl}`
    },
    data: {
  ACTIVITY_NAME: "PersonProfileActivity",
  //The below field name to be same as the one used in the client
  PERSON_UID_INTENT_EXTRA: followerUid,
  PERSON_NAME_INTENT_EXTRA: followerData.name,
  PERSON_USERNAME_INTENT_EXTRA: followerData.userName,
  //If the app is in the foreground then this channel will be used to trigger a notification and this     
//channel has to
  //be created at the client else, this will fail
  CHANNEL_ID: "Follow Update ID"
}
  }

  //random 11 digital Notification Doc Id
  const randomNotificationDocId = (Math.random() * 100000000000).toString()

  const notificationObject = {
message:`${followerData.userName} started following you`,
receivedTime: Date.now(),
//This is needed for client to access this doc and update the wasClicked field
notificationDocId: randomNotificationDocId,
senderName: followerData.name,
senderUid: followerData.uid,
//this will be false by default, will turn true at client when clicked
wasClicked: false,
//this type has to be same as in the client
notificationChannelId: "Follow Updates",
intentToActivity: "PersonProfileActivity",
intentExtrasUid: followerData.uid,
intentExtrasName: followerData.name,
intentExtrasUserName: followerData.userName,
  }

//Add the follower to the followee sub-collection

 admin.firestore().collection('Users').doc(followeeUid).collection('followers').doc(followerUid)
.set(followerData)

//Add the notification doc to the user's notification sub collection
    admin.firestore().collection('Users').doc(followeeUid).collection('Notifications')
.doc(randomNotificationDocId).set(notificationObject)

//Send the notification to the user
return admin.messaging().sendToDevice(followeeNotificationToken,     
notificationPayload).then(function(response: any) {
  console.log("Successfully sent message:", response);
  })
  .catch(function(error: any) {
  console.log("Error sending message:", error);

      })

    })

  })
})

函数按预期完成,但我在 Firebase 控制台收到“函数返回未定义、预期的承诺或值”。有人可以告诉我我在这里做错了什么

【问题讨论】:

  • 您的代码忽略了很多承诺。仅返回最后一个是不够的 - 您必须处理所有这些,然后返回一个在所有异步工作完成时解析的 Promise。
  • 嗨,Doug,我刚刚在 YouTube 上观看了您关于 Cloud Functions 的 Javascript 承诺系列并解决了这个问题 :)

标签: typescript firebase google-cloud-firestore google-cloud-functions firebase-cloud-messaging


【解决方案1】:

试试这个:

Return Promise.all([
    admin.firestore().collection('Users').doc(followeeUid).collection('followers').doc(followerUid).set(followerData)
    admin.firestore().collection('Users').doc(followeeUid).collection('Notifications').doc(randomNotificationDocId).set(notificationObject)
    admin.messaging().sendToDevice(followeeNotificationToken, notificationPayload)
])

如果您想更新 Firestore 或实时数据库中的更多字段,请将它们放入此 return Promise.all([ ... ]) 语句中。

【讨论】:

  • @AnudeepAnanth 你可以尝试删除.then 和.catch 吗?
  • 感谢 Dharmaraj,我为我拥有的最后 3 个函数实现了这个,但我仍然遇到错误,因为文件中还有 2 个其他函数没有被 promise 处理。所以我将它们全部链接起来(总共 5 个)并且错误消失了
【解决方案2】:

好的,所以我在这个文件中总共有 5 个方法。我必须使用 'then()' 方法链接其中的 2 个,并将底部的 3 个方法添加到一个名为 promises 的数组中,并使用 Promise.all(promises) 命令将它们作为 promise 返回。

这是我的最终代码

export const addTheNewFollower = functions.region('asia-east2').firestore.document
('Users/{followerUserId}/following/{followeeUserId}').onCreate((data, context) => {


//get follower and followee Uids for identification
const followeeUid = context.params.followeeUserId
//for identification and notification payload data (Intent Extras for client)
const followerUid = context.params.followerUserId

//Get Follower user details that needs to be duplicated to the Followee's following 
//Sub Coll
//And also added to the notification Payload data
return admin.firestore().collection('Users').doc(followerUid).get().then((doc:{ 
exists: any; data: () => any }) => {

//Extracting this separately as this need not be copied to the Followers sub-    
//collection
const followerImageUrl = doc.data().DOWNLOAD_URL

//This data will be copied to the followers sub collection
const followerData = {
  name:  doc.data().name,
  uid: followerUid,
  userName: doc.data().userName,
}


//get the notification token of the followee to identify & send notification to his 
//device
return admin.firestore().collection('Users').doc(followeeUid)
.collection('notificationToken')
.doc('theNotificationToken').get().then((notificationTokenDoc:{ exists: any; data: () 
=> any }) => {

  const followeeNotificationToken = notificationTokenDoc.data().notificationToken

//Create the Notification Payload content
const notificationPayload = {
notification: {
  title: 'You have a new follower!',
  body: `${followerData.userName}`,
  clickAction: ".People.PersonProfileActivity",
  image: `${followerImageUrl}`
},
data: {
  ACTIVITY_NAME: "PersonProfileActivity",
  //The below field name to be same as the one used in the client
  PERSON_UID_INTENT_EXTRA: followerUid,
  PERSON_NAME_INTENT_EXTRA: followerData.name,
  PERSON_USERNAME_INTENT_EXTRA: followerData.userName,
  //If the app is in the foreground then this channel will be used to trigger a 
notification and this channel has to
  //be created at the client else, this will fail
  CHANNEL_ID: "Follow Update ID"
}
}

//random 11 digital Notification Doc Id
const randomNotificationDocId = (Math.random() * 100000000000).toString()

const notificationObject = {
message:`${followerData.userName} started following you`,
receivedTime: Date.now(),
//This is needed for client to access this doc and update the wasClicked field
notificationDocId: randomNotificationDocId,
senderName: followerData.name,
senderUid: followerData.uid,
//this will be false by default, will turn true at client when clicked
wasClicked: false,
//this type has be same as in the client
notificationChannelId: "Follow Updates",
intentToActivity: "PersonProfileActivity",
intentExtrasUid: followerData.uid,
intentExtrasName: followerData.name,
intentExtrasUserName: followerData.userName,
}


const promises = []
  //Add the follower to the followee sub-collection
  const p = admin.firestore().collection('Users').doc(followeeUid)
.collection('followers').doc(followerUid).set(followerData)
  promises.push(p)
  //Add the notification doc to the user's notification sub collection
  const p1 = admin.firestore().collection('Users').doc(followeeUid)
.collection('Notifications').doc(randomNotificationDocId).set(notificationObject)
  promises.push(p1)
  //Send the notification to the user
  const p2 = admin.messaging().sendToDevice(followeeNotificationToken, 
notificationPayload)
  promises.push(p2)

  return Promise.all(promises)
})

})

})

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-18
    • 2018-12-03
    • 2019-08-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-27
    • 2018-12-05
    • 2019-11-18
    相关资源
    最近更新 更多