Firebase云功能-返回未定义,期望的承诺或价值

时间:2019-03-15 16:10:57

标签: javascript firebase firebase-cloud-messaging google-cloud-functions

我正尝试向用户发送通知,但由于某种原因我遇到了错误。 错误为“函数返回了未定义的,预期的承诺或价值”。

我不明白这行是什么意思,希望你们看看我的代码并告诉我哪里错了。

代码

exports.sendNotification = functions.database.ref('/likes/{videoId}/{currentUser}').onWrite(event =>{
  console.log("Start send like notification");
  const model = event.data.val();
  let ownerVideoUserId = model.userVideoID;
  let username = model.userName;

  if(username == ""){
    username = "Somebody";
  }

  console.log("model notifi: ",model);
  console.log("userVideoID notifi: ",ownerVideoUserId);
  console.log("username notifi: ",username);

  let reference = admin.database().ref("/users/" + ownerVideoUserId);
        reference.once('value').then(snap => {
            //get the token
    let token = snap.child('token').val();
    console.log("user token: ",token);


  const payload = {
    data: {
      title: "Talent",
      message: username + " liked your video",
    }
  }

  return admin.messaging().sendToDevice(token,payload).then(function(res){
    console.log("Successfully sent message:", res);

  }).catch(function(error) {
    console.log("Error sending message:", error);
    });

   })
});

你能发现我的错误吗?

编辑            Exports.sendCommentNotification = functions.database.ref('genresComments / {genre} / {videoId} / {userId}')。onWrite(event => {         console.log(“开始发送评论通知”);         const model = event.data.val();         让ownerVideoUserId = model.ownerVideoID;         让用户名= model.userName;

    if(username == "")
      username = "Somebody";


    console.log("model: ",model);
    console.log("userVideoID: ",ownerVideoUserId);
    console.log("username: ",username);

      let reference = admin.database().ref("/users/" + ownerVideoUserId);
    return reference.once('value').then(snap => {
      //get the token
      let token = snap.child('token').val();
      console.log("user token: ",token);


    const payload = {
      data: {
        title: "Talent",
        message: username + " comment your post",
      }
    }

    return admin.messaging().sendToDevice(token,payload).then(function(res){
      console.log("Successfully sent message:", res);

    }).catch(function(error) {
      console.log("Error sending message:", error);
      });

   }).catch(function(error){
     console.log("Error with the reference:",error);

   }).then(()=>{
    console.log('notification');
  });
 });

2 个答案:

答案 0 :(得分:1)

正如您将在Doug Stevenson的三段视频中看到的有关Firebase视频系列(https://firebase.google.com/docs/functions/video-series/)中的“ JavaScript承诺”一样,您必须在Cloud Function中返回Promise或值,以指示平台它已经完成了。

因此,根据您的情况,您可以执行以下操作(如果您不关心控制台日志记录):

exports.sendNotification = functions.database.ref('/likes/{videoId}/{currentUser}').onWrite(event =>{
  ....

  let reference = admin.database().ref("/users/" + ownerVideoUserId);
  return reference.once('value')
  .then(snap => {
     ...
     return admin.messaging().sendToDevice(token,payload);
  });
});

或如果要保留控制台日志,请执行以下操作:

...
  let reference = admin.database().ref("/users/" + ownerVideoUserId);

  return reference.once('value')
  .then(snap => {
  return admin.messaging().sendToDevice(token,payload);
  .then(function(res){
    console.log("Successfully sent message:", res);
    return null;
  })
  .catch(function(error) {
    console.log("Error sending message:", error);
    return null;
  });
...

答案 1 :(得分:1)

如错误消息所提示,您没有从父级函数返回任何信息……这对于云函数是一个问题。如果您要调用异步代码(例如ref.once('value')方法),则需要返回该代码。

exports.sendNotification = functions.database.ref('/likes/{videoId}/{currentUser}').onWrite(event =>{
    console.log("Start send like notification");
    const model = event.data.val();
    let ownerVideoUserId = model.userVideoID;
    let username = model.userName;

    if(username == ""){
        username = "Somebody";
    }

    console.log("model notifi: ",model);
    console.log("userVideoID notifi: ",ownerVideoUserId);
    console.log("username notifi: ",username);

    let reference = admin.database().ref("/users/" + ownerVideoUserId);

    return reference.once('value').then(snap => {      // <----- RETURN NEEDED HERE
        //get the token
        let token = snap.child('token').val();
        console.log("user token: ",token);

        const payload = {
            data: {
                title: "Talent",
                message: username + " liked your video",
            }
        }

        return admin.messaging().sendToDevice(token,payload).then(function(res){
            console.log("Successfully sent message:", res);
        }).catch(function(error) {
            console.log("Error sending message:", error);
        });

    })
});

编辑:

绝对推荐您查看雷诺在他的回答中推荐的视频系列。那里的解释很好。