我只是按照谷歌云功能教程中的一步一步进程,任何人都知道如何解决这个问题?或问题的原因?

问题描述 投票:0回答:1

我只关注google firebase上的分步教程这是链接:

https://firebase.google.com/docs/functions/get-started?authuser=0

然后我在cmd上得到这些

Function failed on loading user code. Error message: 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
exports.addMessage = functions.https.onRequest(async(req, res) => {
                                                    ^

SyntaxError: Unexpected token (
    at createScript (vm.js:56:10)
    at Object.runInThisContext (vm.js:97:10)
    at Module._compile (module.js:549:28)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at getUserFunction (/var/tmp/worker/worker.js:439:24)


Functions deploy had errors with the following functions:
        addMessage

这些是教程中的代码。

// 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();
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async(req, res) => {
// Grab the text parameter.
const original = req.query.text;
  // Push the new message into the Realtime Database using the Firebase Admin SDK.`enter code here`
  const snapshot = await admin.database().ref('/messages').push({original: 
original});
 // Redirect with 303 SEE OTHER to the URL of the pushed object in the 
Firebase console.
  res.redirect(303, snapshot.ref.toString());
});
node.js firebase google-cloud-functions
1个回答
0
投票

查看您发布的代码与示例中的代码相比,我认为问题是:

你的代码:

exports.addMessage = functions.https.onRequest(async(req, res) => {

示例代码:

exports.addMessage = functions.https.onRequest(async (req, res) => {

请注意,在您的代码中,async之后没有“空格”字符。 Async是函数的限定符,它将函数标记为异步。有关异步的详细信息,请参阅here

遗漏空间会改变语义。如果我不得不猜测,运行时现在将寻找一个名为async的函数。请更改您的代码以包含空格。

© www.soinside.com 2019 - 2024. All rights reserved.