在发现API节点片段之后,似乎存在一个错误:
var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
var discovery = new DiscoveryV1({
username: '{username}',
password: '{password}',
version_date: '2016-12-01'
});
discovery.createCollection(('{environment_id}', '{collection_name}', '{description}' '{configuration_id}'), function(error, data) {
console.log(JSON.stringify(data, null, 2));
});
当我尝试时:
var params;
params = {description:'test',collection_name:'name test',environment_id:'xxxxxxx',
configuration_id:'xxxxxxx'};
discovery.createCollection(params, function(error, data) {
if(error){
console.log(error);
}
console.log(JSON.stringify(data, null, 2));
});
我收到了错误: {错误:集合名称无效或缺失,名称是必需参数
这似乎是一个文档或模块错误。有没有其他人得到同样的错误? 它使用Java Snippet并手动POST / curl
工作正常答案 0 :(得分:0)
nodeJS的Discover API文档中存在一些错误。在每种情况下,(createCollection,query等),API都希望将JSON对象传递给API。因此,createCollection的文档声明调用结构是:
discovery.createCollection(('{environment_id}', '{collection_name}',
'{description}' '{configuration_id}'), function(error, data) {
console.log(JSON.stringify(data, null, 2));
});
呼叫结构应该是:
discovery.createCollection({"environment_id": '{environment_id}', "collection_name": '{collection_name}', "description": '{description}', "configuration_id": '{configuration_id}'}, function(error, data) {
console.log(JSON.stringify(data, null, 2));
});
在你的代码中,你只需要在json术语周围加上引号,而不是这个var语句:
params = {description:'test',collection_name:'name test', environment_id:'xxxxxxx', configuration_id:'xxxxxxx'};
请改用:
params = {"description":'test',"collection_name":'name test', "environment_id":'xxxxxxx', "configuration_id":'xxxxxxx'};
另外,我要小心一个带空格的集合名称....