node-sdk: [Discovery] Error when uploading a JSON document held in a Buffer
The following code demonstrates the problem:
'use strict';
var extend=require('util')._extend;
var fs=require('fs');
var watson=require('watson-developer-cloud');
var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
var discovery = new DiscoveryV1({
username: '(redacted)',
password: '(redacted)',
version_date: DiscoveryV1.VERSION_DATE_2016_12_15
});
var envID= "(redacted)";
var configID= "(redacted)";
var collectionID="(redacted)";
var inputDir = './testdocs';
console.log('input dir: ' + inputDir);
var files = getFiles(inputDir);
console.log('returned files: ' + files.length);
for (var i=0; i<3; i++) {
if(/^\..*/.test(files[i])) {
continue;
}
var fileName = inputDir + '/' + files[i];
console.log('i: ' + i + 'adding file: ' + fileName);
var fileVal = new Buffer(fs.readFileSync(fileName));
var params = {
"environment_id": envID,
"collection_id": collectionID,
"file": fileVal,
"metadata": {'action': 'testing'}
};
console.log('params: ' + JSON.stringify(params));
discovery.addDocument(params, function(error,results) {
if(error) {
console.log('error adding document: ' + JSON.stringify(error));
} else {
console.log('file added successfully');
}
});
}
function getFiles(dir)
{
var allfiles=fs.readdirSync(dir);
console.log('num files: ' + allfiles.length);
return allfiles;
}
I expect this code to read the JSON document, store it in a Buffer, and then upload the buffer to the Discovery service. Instead, it returns this error:
/home/david/git/ccb2-contentbridge/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33
source.on('error', function() {});
^
TypeError: source.on is not a function
at Function.DelayedStream.create (/home/david/git/ccb2-contentbridge/node_modules/request/node_modules/combined-stream/node_modules/delayed-stream/lib/delayed_stream.js:33:10)
at FormData.CombinedStream.append (/home/david/git/ccb2-contentbridge/node_modules/request/node_modules/combined-stream/lib/combined_stream.js:43:37)
at FormData.append (/home/david/git/ccb2-contentbridge/node_modules/request/node_modules/form-data/lib/form_data.js:68:3)
at Request.init.appendFormValue (/home/david/git/ccb2-contentbridge/node_modules/request/request.js:326:21)
at Request.init (/home/david/git/ccb2-contentbridge/node_modules/request/request.js:337:11)
at new Request (/home/david/git/ccb2-contentbridge/node_modules/request/request.js:130:8)
at request (/home/david/git/ccb2-contentbridge/node_modules/request/index.js:54:10)
at createRequest (/home/david/git/ccb2-contentbridge/node_modules/watson-developer-cloud/lib/requestwrapper.js:173:10)
at DiscoveryV1.addDocument (/home/david/git/ccb2-contentbridge/node_modules/watson-developer-cloud/discovery/v1.js:339:10)
at Object.<anonymous> (/home/david/git/ccb2-contentbridge/test.js:50:13)
Node version 4.1.0 SDK version 2.32.1
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 27 (22 by maintainers)
@nfriedly its working, thanks very much π π
Ok, I just released v3.34.0, which fixes the bug so that the above example should work, and also adds a new
addJsonDocumentmethod to make uploading in-memory JSON docs a little more straightforward:I also added about 8 new tests and enabled about a dozen others that had been written before the service was actually released π
So, this should make things a little easier for your use-case and generally more reliable.
@GwilymNewton @buteomont Please test the new release and let me know if itβs working for you now.
@chughts I aware this not the appropriate forum for asking a question.
I already have established how to do what I want, however the code is failing, and believe the reason is the same bug @buteomont is hitting. As hopefully you can see from my stack trace.
I am explaining my usecase as it appears to quite similar to @buteomontβs