简体   繁体   English

我需要帮助来确定watson-developer-cloud Node.js函数的正确语法,以将文档插入Watson的Discovery服务

[英]I need help figuring out the correct syntax for the watson-developer-cloud Node.js function to insert a document into Watson's Discovery service

I'm trying to insert JSON documents into a Discovery collection using the Node.js watson-developer-cloud JDK. 我正在尝试使用Node.js watson-developer-cloud JDK将JSON文档插入Discovery集合中。 This is the relevant code: 这是相关代码:

var DiscoveryV1=require('watson-developer-cloud/discovery/v1');
var discovery=new DiscoveryV1(credentials);

let doc=aSmallValidJsonObject;
let parms={
  environment_id: envID,
  collection_id: collID,
  configuration_id: confID,
  file: {
        value: new Buffer(JSON.stringify(doc)),
        options:{
          contentType:'application/json',
          "Content-Type":'application/json' //just to be sure
        }
     }
  };
discovery.addDocument(parms,
  function(err,results)
    {
    if (err) {...

The error being returned from this call is 此调用返回的错误是

"Error: Request must specify either a "metadata" or "file" part"

I have also tried making parms this way: 我也尝试过通过以下方式制作Parms

let parms={
  environment_id: envID,
  collection_id: collID,
  configuration_id: confID,
  metadata:{"Content-Type":"application/json"},
  file:new Buffer(JSON.stringify(doc))
  };

The error I get in this case is 我在这种情况下得到的错误是

[TypeError: source.on is not a function]

(I traced this error down to line 33 in the library delayed_stream.js ) (我将此错误追溯到库delay_stream.js中的第33行)

If I make the metadata field a string (ie, enclose the value in single quotes) in the above parms, I get this error: 如果在上述参数中将元数据字段设置为字符串(即,将值括在单引号中),则会出现此错误:

Error: The Media Type [application/octet-stream] of the input document is not supported. Auto correction was attempted, but the auto detected media type [text/plain] is also not supported. Supported Media Types are: application/json, application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/pdf, text/html, application/xhtml+xml

Can someone tell me what the correct syntax is for this function? 有人可以告诉我此功能的正确语法是什么吗?

I had the same problem with the watson-developer-cloud SDK and when posting directly to the API without the SDK. 使用watson-developer-cloud SDK和在不使用SDK的情况下直接发布到API时,我遇到了同样的问题。 The only way I could make it work was ignoring the file and sending my document content as part of the metadata directly to the API (example using request package). 使它起作用的唯一方法是忽略文件,并将我的文档内容作为元数据的一部分直接发送到API(例如,使用请求包)。

var options = {
    uri:'https://gateway.watsonplatform.net/discovery/api/v1/environments/'+process.env.DISCOVERY_ENVIRONMENT_ID+'/collections/'+process.env.DISCOVERY_COLLECTION_ID+'/documents?version=2016-12-01&configuration_id='+process.env.DISCOVERY_CONFIGURATION_ID,
    method: 'POST',
    formData: {
        metadata: {
            doc: JSON.stringify(doc)
        }
    },
    auth: {
       user: process.env.DISCOVERY_USERNAME,
       pass: process.env.DISCOVERY_PASSWORD
    },
};

request(options, function(err, httpResponse, body){
    if(err){
      console.log(err);
    }
    console.dir(body);
});

You then need to configure your collection to use the document in the metadata for any enrichments and remember to query against the metadata. 然后,您需要配置您的集合以使用元数据中的文档进行任何扩充,并记住针对元数据进行查询。

Require the FileSystem ( fs ) library to send the document correctly, if you want to send one doc inside your Computer HD. 如果要在Computer HD中发送一个文档,则要求FileSystem( fs )库正确发送文档。 The error seems like the header inside your options isn't recognize. 该错误似乎无法识别您的选项中的标题。 Try the official example or my example: 尝试官方示例或我的示例:

The Official API Reference show one example to addDocument like: 官方API参考显示了addDocument的一个示例,例如:

var DiscoveryV1 = require('watson-developer-cloud/discovery/v1');
var fs = require('fs');

var discovery = new DiscoveryV1({
  username: '{username}',
  password: '{password}',
  version_date: '2017-06-25'
});

var file = fs.readFileSync('{/path/to/file}');

discovery.addDocument(('{environment_id}', '{collection_id}', file),
function(error, data) {
  console.log(JSON.stringify(data, null, 2));
});

With your example: 以您的示例为例:

discovery.addDocument({
    environment_id: yourEnvId,
    collection_id: yourCollId,
    metadata:'{"Content-Type":"application/json"}',
    file: Buffer.from(doc)
}, function(err, data) {
    if (err) {
        return next(err);
    } else {
        return res.json(data)
      }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ibm的watson-developer-cloud Node.js sdk的Discovery模块中的updateDocument api坚持一个file参数 - The updateDocument api in the Discovery module of ibm's watson-developer-cloud Node.js sdk insists on a file parameter 使用Node.js(watson-developer-cloud模块)发送AlchemyData新闻查询 - Sending an AlchemyData News query using Node.js (watson-developer-cloud module) 需要帮助找出音频的语法错误 - Need help figuring out syntax error with audio 从Ibm Watson发现响应下载文档 - Download a document from Ibm watson discovery response Watson Conversation node.js使用learning_opt_out创建工作空间 - Watson Conversation node.js create workspace with learning_opt_out 如何在 node.js 中使用 Watson text-to-speech api? - How to use Watson text-to-speech api in node.js? 需要帮助弄清楚为什么从 mongoose 模式调用方法在我的 node.js 应用程序的一部分工作正常,但在其他地方失败 - Need help figuring out why calling methods from a mongoose schema works fine in one part of my node.js app, but fails elsewhere 我需要帮助弄清楚为什么我在这个函数中得到NaN - I need help figuring out why I'm getting NaN in this function 我需要帮助弄清楚为什么这个“if”语句不起作用 - I need help figuring out why this 'if' statement wont work 在Javascript / Node.Js中设置Watson Speech to Text API时出现问题 - Problems setting up Watson Speech to Text API in Javascript / Node.Js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM