当“时出现意外的函数表达式错误”;“firebase部署”;
我正在使用React Native和Expo。 我正在为我的应用程序执行推送通知。 当用户收到新任务时,它将显示expo的通知 这是Firebase index.js的代码当“时出现意外的函数表达式错误”;“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
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){})相同
来自此
到此
来自此
到此