gpt4 book ai didi

javascript - 在 Firebase 云函数中包含异步函数 (eslint "Parsing error: Unexpected token function")

转载 作者:数据小太阳 更新时间:2023-10-29 05:49:09 26 4
gpt4 key购买 nike

问题

如何将 async 辅助方法添加到 Cloud Functions index.js 文件中?在将 fs.writefile 转换为 Promise 时,需要一个 async 函数才能使用 await ,如本文所述StackOverflow 帖子:fs.writeFile in a promise, asynchronous-synchronous stuff .但是,lint 不赞成在 exports 函数之外向 index.js 文件添加额外的方法。

错误

84 行引用辅助函数 async function writeFile

Users/adamhurwitz/coinverse/coinverse-cloud-functions/functions/index.js 84:7 error Parsing error: Unexpected token function

✖ 1 problem (1 error, 0 warnings)

npm ERR! code ELIFECYCLE

npm ERR! errno 1

npm ERR! functions@ lint: eslint .

npm ERR! Exit status 1

npm ERR!

npm ERR! Failed at the functions@ lint script.

npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:

npm ERR! /Users/adamhurwitz/.npm/_logs/2018-12-12T01_47_50_684Z-debug.log

Error: functions predeploy error: Command terminated with non-zero exit code1

设置

index.js

const path = require('path');
const os = require('os');
const fs = require('fs');
const fsPromises = require('fs').promises;
const util = require('util');
const admin = require('firebase-admin');
const functions = require('firebase-functions');
const {Storage} = require('@google-cloud/storage');
const textToSpeech = require('@google-cloud/text-to-speech');

const storage = new Storage({
projectId: 'project-id',
});
const client = new textToSpeech.TextToSpeechClient();

admin.initializeApp();

exports.getAudiocast = functions.https.onCall((data, context) => {
const bucket = storage.bucket('gs://[bucket-name].appspot.com');
var fileName;
var tempFile;
var filePath;

return client.synthesizeSpeech({
input: {text: data.text },
voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
audioConfig: {audioEncoding: 'MP3'},
})
.then(responses => {
var response = responses[0];
fileName = data.id + '.mp3'
tempFile = path.join(os.tmpdir(), fileName);
return writeFile(tempFile, response.audioContent)
})
.catch(err => {
console.error("Synthesize Speech Error: " + err);
})
.then(() => {
filePath = "filePath/" + fileName;
return bucket.upload(tempFile, { destination: filePath })
})
.catch(err => {
console.error("Write Temporary Audio File Error: " + err);
})
.then(() => {
return { filePath: filePath }
})
.catch(err => {
console.error('Upload Audio to GCS ERROR: ' + err);
});
});

辅助方法:

async function writeFile(tempFile, audioContent) {
await fs.writeFile(tempFile, audioContent, 'binary');
}

尝试的解决方案

按照博文 Cloud Functions for Firebase Async Await style 中的建议启用 Node.js 8 .

  1. Set Node.js version“引擎”:{“Node ”:“8”}

  2. return await fs.writeFile(tempFile, audioContent, 'binary');

Lint 不喜欢这种解决方案。

最佳答案

我尝试了以上所有对我不起作用的解决方案。这是由于我的 package.json 语法错误:

"scripts": {
"lint": "eslint ."
},

改为:

"scripts": {
"lint": "eslint"
},

正如 Burak 在评论中所说,这个点是在我们创建 firebase 函数时默认放置的

关于javascript - 在 Firebase 云函数中包含异步函数 (eslint "Parsing error: Unexpected token function"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53735009/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com