在Windows命令中转义双引号 [英] Escaping double quotes in windows commands

查看:352
本文介绍了在Windows命令中转义双引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Jest运行示例测试,以验证我的Google Cloud Function是否正常运行,但是我一直在跟踪错误。

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.


错误:命令失败:gcloud beta函数调用cf-1 --region europe-west1 --data'{ data: eyJkYXRhIjoiMSJ9}'

错误:(gcloud.beta .functions.call)[--data]的值无效:不是有效的JSON:无法解码JSON对象

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

I知道在Windows终端中运行命令时我可以使用反斜杠转义双引号,但如何在JavaScript中执行。

I know that i can escape double quotes with backslash when running the command in windows terminal but how to do it in JavaScript.

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


推荐答案

尝试两次:

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

这篇关于在Windows命令中转义双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆