我想分析使用 Watson 音调分析器动态创建的 JSON 文件。我希望它读取文件,然后分析它。
如何让tone_analyzer.tone方法读取文件?谢谢。
app.get('/results', function(req, res) {
// This is the json file I want to analyze
fs.readFile('./output.json', null, cb);
function cb() {
tone_analyzer.tone({
// How can I pass the file here?
text: ''
},
function(err, tone) {
if (err)
console.log(err);
else
console.log(JSON.stringify(tone, null, 2));
});
console.log('Finished reading file.')
}
res.render('results');
})
最佳答案
您的回调缺少几个参数(错误、数据)(有关详细信息,请参阅 the node fs documentation)。数据是文件的内容,将发送到您发送文本的位置。
尝试这样的事情:
app.get('/results', function(req, res) {
// This is the json file I want to analyze
fs.readFile('./output.json', 'utf8', cb);
function cb(error, data) {
if (error) throw error;
tone_analyzer.tone({
// How can I pass the file here?
text: data
},
function(err, tone) {
if (err)
console.log(err);
else
console.log(JSON.stringify(tone, null, 2));
});
console.log('Finished reading file.')
}
res.render('results');
})
关于json - 使用 Watson API Nodejs 分析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879436/