我正在尝试为我的react原生应用程序部署一个云函数,当数据库中出现新节点时,该应用程序会向用户发送通知。为此,我使用了Expo的Push API,如下所示:https://docs.expo.io/versions/v32.0.0/guides/push-notifications,并遵循此处提供的教程:https://www.youtube.com/watch?v=R2D6J10fhA4
我已经能够很好地获取设备令牌并将它们保存到数据库中。但是,我无法将该函数部署到我的数据库中,因为出现了这样的错误:
31:27分析错误:意外的标记,预期的,
它在'body: JSON.stringify(messages)‘行抛出了一个致命错误,就好像它期望'stringify’后面紧跟一个逗号一样。我非常不确定如何从这里开始,似乎找不到任何关于这个特定问题的帖子。
任何帮助和/或建议都是非常感谢的!谢谢。
const functions = require('firebase-functions');
let fetch = require('node-fetch');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendPushNotification = functions.database.ref('Omicron-Pi')
.onCreate(event => {
const root = event.data.ref.root;
let messages = [];
root.child('Omicron-Pi/profiles').once('value').then((snapshot) => {
snapshot.forEach((childSnapshot) => {
let pushToken = childSnapshot.val().pushToken;
if (pushToken) {
messages.push({
to: pushToken,
body: 'New Node added'
});
}
});
return Promise.all(messages);
}).then(messages => {
fetch('https://exp.host/--/api/v2/push/send', [
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(messages)
]);
});
});发布于 2019-01-22 00:27:34
fetch()的第二个参数是一个具有初始化属性的对象,但您传递的是一个数组。
要修复它,请使用{}而不是[]
fetch('https://exp.host/--/api/v2/push/send', {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify(messages)
});要了解更多信息,请阅读documentation on how to use fetch on MDN。
https://stackoverflow.com/questions/54293872
复制相似问题