【问题标题】:Invalid json while trying gcloud functions commands via javascript通过javascript尝试gcloud函数命令时json无效
【发布时间】:2020-01-18 09:59:29
【问题描述】:

我正在尝试使用 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

测试文件

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

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

【问题讨论】:

  • 这不能回答您的问题,但请注意,您不能从 Cloud Functions 中运行的代码调用 gcloud
  • @JohnHanley 我的意图不是从云函数中调用 gcloud。我正在关注google 的一篇文章,该文章深入了解了测试云功能,因此我将在本地运行代码并测试部署在云中的云功能。

标签: javascript google-cloud-platform jestjs google-cloud-functions child-process


【解决方案1】:

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

在 Windows 上将 --data 部分更改为此:

--data "{\"data\":\"eyJkYXRhIjoiMSJ9\"}"

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

【讨论】:

  • 谢谢,我错过了提到我知道如何从 cmd 行使其工作但不知何故我找不到任何方法在 javascript 中做同样的事情
  • 您需要更改您的问题以匹配您需要的答案。你的问题是关于无效的 JSON,我回答了。
【解决方案2】:

我遇到了同样的问题,尤其是当我想在我的数据参数中传递多个变量时:

发送以下 JSON:

 {"key":"val", "key2":"val2"}

我附加如下

gcloud functions call function-name --data "{\"key\":\"val\", \"key2\":\"val2\"}"

【讨论】:

    【解决方案3】:

    我已尝试通过执行以下操作来复制您的情况和您遇到的问题:

    首先,我使用 Node.js 8 部署了一个简单的 Cloud Function

    /**
     * Responds to any HTTP request.
     *
     * @param {!express:Request} req HTTP request context.
     * @param {!express:Response} res HTTP response context.
     */
    exports.simpleFunction = (req, res) => {
      let data = req.query.data || req.body.data || 'Didn\'t work!';
      res.status(200).send(data);
    };
    

    之后,我按照 Google Cloud Platform Quickstart guide 创建了一个简单的 Node.js 应用程序,然后我对其进行了修改以在本地调用我的函数。我已经添加了一些代码行,您在示例中可以调用该函数:

    'use strict';
     
    // [START gae_node_request_example]
    const express = require('express');
    const childProcess = require('child_process');
     
    const app = express();
    
    // The lines you used for a function call 
    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 b-func --region us-central1 --data '${data}'`).toString();
     
     
    app.get('/', (req, res) => {
      res
        .status(200)
        .send(executeResultOutput)
        .end();
    });
     
    // Start the server
    const PORT = process.env.PORT || 8080;
    app.listen(PORT, () => {
      console.log(`App listening on port ${PORT}`);
      console.log('Press Ctrl+C to quit.');
    });
    // [END gae_node_request_example]
     
    module.exports = app;
    

    上面的代码对我有用,这是我收到的输出

    executionId: 6n6pf9720dsj result: eyJtZXNzYWdlIjoiMSJ9
    

    我唯一能得到与你相同的 ERROR 的情况:Is not a valid JSON: No JSON object could be decoded 是当我传递 数据值 时没有用引号括起来:

    const data = {"data":"some data"};
    const executeResultOutput = childProcess.execSync(`gcloud beta functions call b-func --region us-central1 --data '${data}'`).toString();
    

    编辑:

    我已经在 Linux 环境中测试了我上面提到的代码。一旦我尝试在 Windows 中运行它,我得到了同样的错误。

    由于代码在本地运行,PowerShell 仍然需要像 John 指出的那样转义参数。

    我看到您已经针对此问题打开了一个新的question。试试那里提供的解决方案 - 它对我有用。

    【讨论】:

    • 我会很快再试一次,并会告诉我我的代码有什么问题。顺便说一句,你是在 Windows 上运行的吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    • 1970-01-01
    • 2013-06-28
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多