我用这本书来构建我的第一个基于NodeJS的NLU应用程序,它可以完美运行:http://www.redbooks.ibm.com/redbooks.nsf/redbookabstracts/sg248398.html?Open
但是我被困在将JSON响应保存到文件中...
我有一个处理调用的js文件(articleServices.js),首先定义参数
var parameters = {
"url": req.body.url,
"features": {
"entities": {
"model": "c9c18e41-2f36-4ee5-ad59-9a3b3243c513"
},
"relations": {
"model": "c9c18e41-2f36-4ee5-ad59-9a3b3243c513"
}
}
};
...并将此参数调用给Watson做到了神奇...
var parameters = {
"url": req.body.url,
"features": {
"entities": {
"model": "c9c18e41-2f36-4ee5-ad59-9a3b3243c513"
},
"relations": {
"model": "c9c18e41-2f36-4ee5-ad59-9a3b3243c513"
}
}
};
...并全部嵌套在其中:
exports.extractArticleAuthorNames = function(req, callback){ ... }
作为非开发人员,我的逻辑指向我嵌套代码以将json文件保存在该文件中,该文件将定义响应的路径。所以我写了这个文件(author.js):
之前:
var express = require('express');
var router = express.Router();
var articleServices = require('../services/articleServices');
router.post('/', function(req, res) {
articleServices.extractArticleAuthorNames(req, function(err, response) {
if (err)
res.status(500).send('error: ' + err);
else
res.send(response)
});
});
module.exports = router;
之后
var express = require('express');
var router = express.Router();
var articleServices = require('../services/articleServices');
var fs = require('fs');
router.post('/', function(req, res) {
articleServices.extractArticleAuthorNames(req, function(err, response) {
if (err)
res.status(500).send('error: ' + err);
else
res.send(response)
});
var jsonData = ("../author");
fs.writeFile("../test/primo.json", jsonData, (error) => {
if(erro) {
throw erro;
}
console.log("Worked");
});
});
module.exports = router;
而且...没有用:-(
错误日志:
ReferenceError:响应未定义
at /home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/routes/author.js:16:17
at Layer.handle [as handle_request] (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/layer.js:95:5)
at next (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/layer.js:95:5)
at /home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/index.js:335:12)
at next (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/index.js:174:3)
at router (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/router/index.js:47:12)
(节点:21477)UnhandledPromiseRejectionWarning:错误:发送标头后无法设置标头。
at validateHeader (_http_outgoing.js:491:11)
at ServerResponse.setHeader (_http_outgoing.js:498:3)
at ServerResponse.header (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/response.js:767:10)
at ServerResponse.send (/home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/express/lib/response.js:170:12)
at /home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/routes/author.js:11:18
at /home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/services/articleServices.js:34:5
at /home/luizc/Área de Trabalho/code_projs/JurisPrimo_Dev/node_modules/watson-developer-cloud/node_modules/ibm-cloud-sdk-core/lib/requestwrapper.js:259:9
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7)
(节点:21477)UnhandledPromiseRejectionWarning:未处理的承诺拒绝。引发此错误的原因可能是抛出了一个没有catch块的异步函数,或者是拒绝了一个.catch()无法处理的承诺。 (拒绝ID:1)
(节点:21477)[DEP0018] DeprecationWarning:已弃用未处理的承诺拒绝。将来,未处理的承诺拒绝将以非零退出代码终止Node.js进程。
======================================
你们伙计们可以给我提示解决这个问题吗?
非常感谢!