node.js - How to index a JSON document directly from memory -


i'm trying index json document, isn't working; far, have tried solutions posted in https://developer.ibm.com/answers/questions/361808/adding-a-json-document-to-a-discovery-collection-u/ , not work;

if try:

    discovery.adddocument({         environment_id: config.watson.environment_id,         collection_id: config.watson.collection_id,         file: json.stringify({             "ocorrencia_id": 9001         })     }, (error, data) => {         if (error) {             console.error(error);             return;         }          console.log(data);     }); 

it returns me error:

    media type [text/plain] of input document not supported. auto correction attempted, auto detected media type [text/plain] not supported. supported media types are: application/json, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, text/html, application/xhtml+xml . 

on other hand, if try:

    discovery.adddocument({         environment_id: config.watson.environment_id,         collection_id: config.watson.collection_id,         file: json.parse(json.stringify({             "ocorrencia_id": 9001         }))     }, (error, data) => {         if (error) {             console.error(error);             return;         }          console.log(data);     }); 

i error:

typeerror: source.on not function     @ function.delayedstream.create (c:\temp\teste-watson\watson-orchestrator\node_modules\delayed-stream\lib\delayed_stream.js:33:10)     @ formdata.combinedstream.append (c:\temp\teste-watson\watson-orchestrator\node_modules\combined-stream\lib\combined_stream.js:43:37)     @ formdata.append (c:\temp\teste-watson\watson-orchestrator\node_modules\form-data\lib\form_data.js:68:3)     @ appendformvalue (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:324:21)     @ request.init (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:337:11)     @ new request (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:130:8)     @ request (c:\temp\teste-watson\watson-orchestrator\node_modules\request\index.js:54:10)     @ createrequest (c:\temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\lib\requestwrapper.js:177:10)     @ discoveryv1.adddocument (c:\temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\discovery\v1.js:516:10)     @ client.query.then.res (c:\temp\teste-watson\watson-orchestrator\populate\populate.js:36:13)     @ process._tickcallback (internal/process/next_tick.js:109:7) 

similarly, saving temp file, , using it:

    const tempy = require('tempy');     const f = tempy.file({extension: 'json'});     fs.writefilesync(f, json.stringify({             "ocorrencia_id": 9001     }));      discovery.adddocument({         environment_id: config.watson.environment_id,         collection_id: config.watson.collection_id,         file: fs.readfilesync(f)     }, (error, data) => {         if (error) {             console.error(error);             return;         }          console.log(data);     }); 

then happens:

the media type [application/octet-stream] of input document not supported. auto correction attempted, auto detected media type [text/plain] not supported. supported media types are: application/json, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, text/html, application/xhtml+xml . 

considering other posts recommend using json.parse(), seems api accepts js object, none of examples, , nothing have tried far seems working. seems bug?

update: saving temp file , using createdatastream(), instead of readfilesync(), works, still big bother having got through disk information that's on memory.

i have tried create in-memory stream readable, fails, too:

    var readable = require('stream').readable;     var s = new readable();     s._read = function noop() {}; // redundant? see update below     s.push(json.stringify({             "ocorrencia_id": 9001     }));             s.push(null);      discovery.adddocument({         environment_id: config.watson.environment_id,         collection_id: config.watson.collection_id,         file: s     }, (error, data) => {         if (error) {             console.error(error);             return;         }          console.log(data);     }); 

this 1 fails with:

error: unexpected end of multipart data     @ request._callback (c:\temp\teste-watson\watson-orchestrator\node_modules\watson-developer-cloud\lib\requestwrapper.js:88:15)     @ request.self.callback (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:188:22)     @ emittwo (events.js:106:13)     @ request.emit (events.js:191:7)     @ request.<anonymous> (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:1171:10)     @ emitone (events.js:96:13)     @ request.emit (events.js:188:7)     @ gunzip.<anonymous> (c:\temp\teste-watson\watson-orchestrator\node_modules\request\request.js:1091:12)     @ gunzip.g (events.js:292:16)     @ emitnone (events.js:91:20)     @ gunzip.emit (events.js:185:7)     @ endreadablent (_stream_readable.js:974:12)     @ _combinedtickcallback (internal/process/next_tick.js:80:11)     @ process._tickcallback (internal/process/next_tick.js:104:9) code: 500, error: 'unexpected end of multipart data' 

the service checks filename , content determine type, doesn't seem recognize json json - sees text. other answer work, long filename ends in .json (it not care contenttype).

however, added .addjsondocument() , .updatejsondocument() methods make easier:

discovery.addjsondocument({     environment_id: config.watson.environment_id,     collection_id: config.watson.collection_id,      // note: no json.stringify needed addjsondocument()     file: {          "ocorrencia_id": 9001     } }, (error, data) => {     if (error) {         console.error(error);         return;     }      console.log(data); }); 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -