【问题标题】:ERROR Unexpected function expression when "firebase deploy"错误“firebase deploy”时出现意外的函数表达式
【发布时间】:2019-07-01 06:31:47
【问题描述】:

我正在使用 React-Native 和 Expo。 我正在为我的应用程序做推送通知。 当用户收到新任务时,会通过 expo 显示通知。

这是 Firebase index.js 的代码

const functions = require('firebase-functions');
var fetch = require('node-fetch')



const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

//send the push notification 
exports.sendPushNotification = 
functions.database.ref('Task/').onCreate(event => {

const root = event.data.ref.root
var messages = []

//return the main promise 
return root.child('/users').once('value').then(function (snapshot) {
    snapshot.forEach(function (childSnapshot) {

        var expoToken = childSnapshot.val().expoToken;

        messages.push({
            "to": expoToken,
            "sound": "default",
            "body": "New Task Added"
        });
    });
    //firebase.database then() respved a single promise that resolves
    //once all the messages have been resolved 
    return Promise.all(messages)

})
    .then(messages => {
        // console.log(messages)
        fetch('https://exp.host/--/api/v2/push/send', {
            method: 'POST',
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json',
            },
            body: JSON.stringify(messages)

        });
    })
    .catch(reason => {
        console.log(reason)
    })


});

我在执行“firebase deploy”时收到以下错误

谁能帮帮我,谢谢。

【问题讨论】:

    标签: firebase react-native firebase-realtime-database google-cloud-functions expo


    【解决方案1】:

    查看错误消息,似乎错误来自您在以下then 中没有返回任何内容:

    .then(messages => {
            // console.log(messages)
            fetch(...);
    })
    

    我从未使用过 node-fetch,但根据文档,您似乎只需返回 fetch() 返回的承诺,如下所示:

    .then(messages => {
            // console.log(messages)
            return fetch(...);
    })
    

    请注意,这不仅从 eslint 的角度来看很重要,而且对于正确执行云函数也很重要。我建议您观看 Firebase 视频系列中关于“JavaScript Promises”的 3 个视频:https://firebase.google.com/docs/functions/video-series/


    最后,请注意 eslint 还会在 using arrow functions for callbacks 上向您发出警告。

    你可以修改下面这段代码

    return root.child('/users').once('value').then(function (snapshot) {})
    

    return root.child('/users').once('value').then(snapshot => {})
    

    snapshot.forEach(function (childSnapshot) {}) 也一样。

    【讨论】:

      【解决方案2】:

      从此


       facts = queryRecords().then(function(result) {
          return result.data 
      }).catch(function(error) {
          facts = error 
      });
      

      到此


       facts = queryRecords().then(result => {
          return result.data 
      }).catch(error => {
          facts = error 
      });
      

      【讨论】:

        猜你喜欢
        • 2021-01-13
        • 2012-10-14
        • 2020-06-18
        • 2018-05-04
        • 2020-07-06
        • 2019-11-07
        • 2020-11-08
        • 1970-01-01
        • 2019-09-30
        相关资源
        最近更新 更多