[英]The updateDocument api in the Discovery module of ibm's watson-developer-cloud Node.js sdk insists on a file parameter
[英]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
我正在尝试使用Node.js watson-developer-cloud JDK将JSON文档插入Discovery集合中。 这是相关代码:
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) {...
此调用返回的错误是
"Error: Request must specify either a "metadata" or "file" part"
我也尝试过通过以下方式制作Parms :
let parms={
environment_id: envID,
collection_id: collID,
configuration_id: confID,
metadata:{"Content-Type":"application/json"},
file:new Buffer(JSON.stringify(doc))
};
我在这种情况下得到的错误是
[TypeError: source.on is not a function]
(我将此错误追溯到库delay_stream.js中的第33行)
如果在上述参数中将元数据字段设置为字符串(即,将值括在单引号中),则会出现此错误:
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
有人可以告诉我此功能的正确语法是什么吗?
使用watson-developer-cloud SDK和在不使用SDK的情况下直接发布到API时,我遇到了同样的问题。 使它起作用的唯一方法是忽略文件,并将我的文档内容作为元数据的一部分直接发送到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);
});
然后,您需要配置您的集合以使用元数据中的文档进行任何扩充,并记住针对元数据进行查询。
如果要在Computer HD中发送一个文档,则要求FileSystem( fs
)库正确发送文档。 该错误似乎无法识别您的选项中的标题。 尝试官方示例或我的示例:
官方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));
});
以您的示例为例:
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.