Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
当“时出现意外的函数表达式错误”;“firebase部署”;_Firebase_React Native_Firebase Realtime Database_Google Cloud Functions_Expo - Fatal编程技术网

当“时出现意外的函数表达式错误”;“firebase部署”;

当“时出现意外的函数表达式错误”;“firebase部署”;,firebase,react-native,firebase-realtime-database,google-cloud-functions,expo,Firebase,React Native,Firebase Realtime Database,Google Cloud Functions,Expo,我正在使用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

我正在使用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部署”时出现以下错误


有人能帮我一下吗,谢谢。

查看错误消息,似乎是因为您没有在下面的
中返回任何内容,然后

.then(messages => {
        // console.log(messages)
        fetch(...);
})
我从未使用过node fetch,但根据文档,您似乎只需要返回
fetch()
返回的承诺,如下所示:

.then(messages => {
        // console.log(messages)
        return fetch(...);
})
请注意,这不仅从eslint的角度来看很重要,而且对于云功能的正确执行也很重要。我建议您观看Firebase视频系列中关于“JavaScript承诺”的3个视频:


最后,请注意,eslint还为您提供了有关的警告

您可以修改以下代码

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


对于
snapshot.forEach(function(childSnapshot){})
查看错误消息,错误似乎来自以下
中没有返回任何内容的事实,然后

.then(messages => {
        // console.log(messages)
        fetch(...);
})
我从未使用过node fetch,但根据文档,您似乎只需要返回
fetch()
返回的承诺,如下所示:

.then(messages => {
        // console.log(messages)
        return fetch(...);
})
请注意,这不仅从eslint的角度来看很重要,而且对于云功能的正确执行也很重要。我建议您观看Firebase视频系列中关于“JavaScript承诺”的3个视频:


最后,请注意,eslint还为您提供了有关的警告

您可以修改以下代码

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

与snapshot.forEach(函数(childSnapshot){})相同


来自此



到此



来自此



到此