Javascript 正在创建firebase函数,google示例不起作用
我试图创建一个云函数,在我写入数据库时发送通知,我对javascript几乎一无所知,所以我尝试使用一个google示例,但它们使用的语法是我的node.js不喜欢的,因此我更新了工具,并根据我的package.json添加了node 8引擎,但我在尝试部署此函数时仍然出错错误是Javascript 正在创建firebase函数,google示例不起作用,javascript,android,function,firebase,google-cloud-functions,Javascript,Android,Function,Firebase,Google Cloud Functions,我试图创建一个云函数,在我写入数据库时发送通知,我对javascript几乎一无所知,所以我尝试使用一个google示例,但它们使用的语法是我的node.js不喜欢的,因此我更新了工具,并根据我的package.json添加了node 8引擎,但我在尝试部署此函数时仍然出错错误是 Function load error: Code in file index.js can't be loaded. Is there a syntax error in your code? Detailed st
Function load error: Code in file index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: /user_code/index.js:9
.onWrite(async (change, context) => {
^
有人能帮我解决这个问题吗?这是我的整个index.js脚本
// The Cloud Functions for Firebase SDK to create Cloud Functions and setup
triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
exports.sendNotification =
functions.database
.ref('/user/{userId}/contacts/{contactId}/messages/{messageId}')
.onWrite(async (change, context) => {
const senderId = context.params.userId;
const recipientId = context.params.contactId;
// If update or delete we exit the function
if (!change.after.val()) {
return console.log('User updated or deleted');
}
//get the message
const message = change.data.current.val();
console.log('We have a new message for ', recipientId, ' from user: ', senderId);
// Get the recipient profile.
const getRecipientProfile = await admin.auth().getUser(recipientId);
//get token
const token = getRecipientProfile.registeredToken;
// Notification details.
const payload = {
notification: {
title: 'New message',
body: message.message,
icon: getRecipientProfile.user_small_image
}
};
// Send notification
admin.messaging().sendToDevice(token, payload)
.then(function (response) {
console.log("Successfully sent message:", response);
})
.catch(function (error) {
console.log("Error sending message:", error);
});
});
还有我的package.json
{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"serve": "firebase serve --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "8"
},
"dependencies": {
"firebase-admin": "~5.12.1",
"firebase-functions": "^1.0.3"
},
"private": true
}
你能试着换衣服吗
.onWrite(async (change, context) =>
为了
基本上删除
async
这和删除await使我能够部署函数,这非常感谢。问题是在某些情况下,您绝对需要async/await。。。从代码中删除它会导致函数不再工作。我对谷歌云函数示例也有同样的问题:。删除async/await并不能解决问题。部署成功,但功能不起作用。我也遇到了同样的问题。但是,删除async和Wait会中断该函数。
.onWrite((change, context) =>