通过 javascript 尝试 gcloud 函数命令时 json 无效

Nik*_*pta 4 javascript child-process google-cloud-platform jestjs google-cloud-functions

我正在尝试使用 Jest 运行示例测试来验证我的 Google Cloud 功能是否正常工作,但我不断收到以下错误。

我尝试直接在 Windows Power shell 中运行以下命令,但仍然出现相同的错误,可以在双引号之前使用 \ 来转义。

Error: Command failed: gcloud beta functions call cf-1 --region europe-west1 --data '{"data":"eyJkYXRhIjoiMSJ9"}'
ERROR: (gcloud.beta.functions.call) Invalid value for [--data]: Is not a valid JSON: No JSON object could be decoded
Run Code Online (Sandbox Code Playgroud)

测试文件

const childProcess = require('child_process');

describe('Test CF', () => {
    it('print outs the error message when received JSON is blank', done => {
        const msg = { data: '1' };
        const encodedMsg = Buffer.from(JSON.stringify(msg)).toString('base64');
        const data = JSON.stringify({ data: encodedMsg });
        const executeResultOutput = childProcess.execSync(`gcloud beta functions call cf-1 --region europe-west1 --data '${data}'`).toString();

        const logs = childProcess
            .execSync(
                `gcloud functions logs read cf-1 --region europe-west1 --execution-id ${executionIdObj}`,
            )
            .toString();

        expect(logs).toEqual(expect.stringContaining('Error..'));
    });
});
Run Code Online (Sandbox Code Playgroud)

更新#1:仍然是同样的问题...

在此输入图像描述

Joh*_*ley 6

您使用的命令行语法适用于 Linux。

--data对于 Windows,将此部分更改为:

--data "{\"data\":\"eyJkYXRhIjoiMSJ9\"}"
Run Code Online (Sandbox Code Playgroud)

请注意,您必须转义引号。