通过node.js进行的Google Cloud Search查询错误:收到无效的JSON有效负载

时间:2019-06-14 13:20:03

标签: google-api google-api-nodejs-client

googleapis node.js库在尝试查询云搜索API时返回以下错误。

Error: Invalid JSON payload received. Unknown name \"requestOptions[searchApplicationId]\": Cannot bind query parameter. Field 'requestOptions[searchApplicationId]' could not be found in request message."

有效载荷与此处https://developers.google.com/cloud-search/docs/reference/rest/v1/query/search完全相同。存在requestOptions [searchApplicationId],如果将其删除,则会出现错误消息,提示需要searchApplicationId。

代码:

const {google} = require('googleapis');

const service = google.cloudsearch({version: 'v1'});
service.query.search({
  auth: jwtClient,
  requestOptions: {
    searchApplicationId: 'searchapplications/default',
    debugOptions:{enableDebugging: true}
  },
  query: 'My query'
}).then((res) => {
  console.log(JSON.stringify({results:res.results.length}));
  console.log(JSON.stringify({resultsInfo:res.results[0]}));
}).catch((err) => {
  console.error('Unexpected error with cloud search API.');
  console.error(err.toString());
});

该查询可在API资源管理器中使用。 https://developers.google.com/apis-explorer/#search/cloudsearch/m/cloudsearch/v1/cloudsearch.query.search?_h=1&resource=%257B%250A++%2522requestOptions%2522%253A+%250A++%257B%250A++++%2522searchApplicationId%2522%253A+%2522searchapplications%252Fdefault%2522%250A++%257D%252C%250A++%2522query%2522%253A+%2522Testing%2522%250A%257D&

我想念一些简单的东西吗?这是Google客户端库的问题吗? (https://github.com/googleapis/google-api-nodejs-client)我们将不胜感激。

1 个答案:

答案 0 :(得分:0)

最后弄清楚了。必须将请求包装在 requestBody JSON中。

service.query.search({
  auth: jwtClient,
  requestBody: {
    requestOptions: {
      searchApplicationId: 'searchapplications/default',
      debugOptions:{enableDebugging: true}
    },
    query: 'My query'
  }
})