【问题标题】:Creating firebase functions, google sample not working创建firebase函数,谷歌示例不起作用
【发布时间】:2018-08-15 17:05:49
【问题描述】:

我正在尝试创建一个云函数,当我写入我的数据库时发送通知,我对 javascript 几乎一无所知,所以我尝试使用一个谷歌示例 here 但他们使用我的语法node.js 不喜欢,所以我更新了工具并根据this 将 node 8 引擎添加到我的 package.json 中,但尝试部署此功能时仍然出现错误,错误是

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
}

【问题讨论】:

    标签: javascript android function firebase google-cloud-functions


    【解决方案1】:

    你可以尝试改变吗

    .onWrite(async (change, context) => 
    

    .onWrite((change, context) => 
    

    基本删除async

    【讨论】:

    • 这个和删除 await 让我能够部署这些功能,非常感谢
    • 问题在于,在某些情况下,您绝对需要 async/await ...并且从代码中删除它会导致您的函数不再工作。我对 Google Cloud Function 示例有同样的问题:github.com/firebase/functions-samples/tree/Node-8/…。删除 async/await 并不能解决问题。部署成功,但功能不起作用。
    • 我也遇到了同样的问题。但是删除 async 和 await 会破坏该功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    相关资源
    最近更新 更多