简体   繁体   English

在Windows命令中转义双引号

[英]Escaping double quotes in windows commands

I am trying to run a sample test using Jest to verify if my Google Cloud Function is working fine or not but I am constantly getting following error. 我正在尝试使用Jest运行样本测试,以验证我的Google Cloud Function是否正常运行,但是我一直在跟踪错误。

Error: Command failed: gcloud beta functions call cf-1 --region europe-west1 --data '{"data":"eyJkYXRhIjoiMSJ9"}' 错误:命令失败:gcloud beta函数调用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 错误:(gcloud.beta.functions.call)[--data]的值无效:不是有效的JSON:无法解码JSON对象

I know that i can escape double quotes with backslash when running the command in windows terminal but how to do it in JavaScript. 我知道在Windows终端中运行命令时可以用反斜杠转义双引号,但如何在JavaScript中执行。

test.js test.js

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..'));
    });
});

Try it twice: 尝试两次:

 data = {"data":"eyJkYXRhIjoiMSJ9"} console.log( JSON.stringify(JSON.stringify(data)) ) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM