通过 javascript 尝试 gcloud 函数命令时无效 json
Invalid json while trying gcloud functions commands via javascript
我正在尝试 运行 使用 Jest 进行示例测试以验证我的 Google 云功能是否正常工作,但我不断收到以下错误。
我已经尝试 运行 直接在 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:还是一样的问题...
您使用的命令行语法适用于 Linux。
将 --data
部分更改为此 Windows:
--data "{\"data\":\"eyJkYXRhIjoiMSJ9\"}"
请注意,您必须转义引号。
我已尝试通过执行以下操作来复制您的情况和遇到的问题:
首先,我使用 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云平台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 指出的那样转义参数。
据我所知,您已经就此问题打开了一个新 。尝试那里提供的解决方案 - 它对我有用。
我遇到了同样的问题,尤其是当我想在我的数据参数中传递多个变量时:
发送以下JSON:
{"key":"val", "key2":"val2"}
我附加如下
gcloud functions call function-name --data "{\"key\":\"val\", \"key2\":\"val2\"}"
我正在尝试 运行 使用 Jest 进行示例测试以验证我的 Google 云功能是否正常工作,但我不断收到以下错误。
我已经尝试 运行 直接在 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:还是一样的问题...
您使用的命令行语法适用于 Linux。
将 --data
部分更改为此 Windows:
--data "{\"data\":\"eyJkYXRhIjoiMSJ9\"}"
请注意,您必须转义引号。
我已尝试通过执行以下操作来复制您的情况和遇到的问题:
首先,我使用 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云平台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 指出的那样转义参数。
据我所知,您已经就此问题打开了一个新
我遇到了同样的问题,尤其是当我想在我的数据参数中传递多个变量时:
发送以下JSON:
{"key":"val", "key2":"val2"}
我附加如下
gcloud functions call function-name --data "{\"key\":\"val\", \"key2\":\"val2\"}"