1

Following discovery API Node snippet it seems to exists a bug:

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

When I tried:

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

I got the error: { Error: Invalid or missing collection name, name is a required parameter

It seems to be a documentation or module bug. Did anyone else got the same error? It is working fine using Java Snippet and manually POST/curl

1 Answers1

0

There are some errors in the Discover API documentation for nodeJS. In each case, (createCollection, query, etc), the API is expecting a JSON object to be delivered to the API. So where the documentation for createCollection states that the call structure is:

discovery.createCollection(('{environment_id}', '{collection_name}', 
'{description}' '{configuration_id}'), function(error, data) {
 console.log(JSON.stringify(data, null, 2));
});

The call structure really should be:

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

In your code, you just need to put quotes around the json terms, so instead of this var statement:

params = {description:'test',collection_name:'name test', environment_id:'xxxxxxx', configuration_id:'xxxxxxx'};

Use this instead:

params = {"description":'test',"collection_name":'name test', "environment_id":'xxxxxxx', "configuration_id":'xxxxxxx'};

Separately, I'd be careful with a collection name with spaces ....

Bob Dill
  • 1,000
  • 5
  • 13