我正在尝试使用Node.js应用程序从SAP系统中获取数据。
请建议在我的代码中将async/await
放在哪里,或者使用不同的方法?
我已经尝试了很多不同的方法,但是invoke仍然不等待connect完成:(
var express = require('express');
var router = express.Router();
var rfc = require('node-rfc')
const client = new rfc.Client({
'user': 'us3rname',
'passwd': 'passw0rd',
'ashost': 'h0st',
'sysnr': '00',
'client': '001'
})
router.get('/', function(req, res, next) {
client.connect(err => err ? (console.log(err)) : (console.log('Connection successful')))
client.invoke('RFC_READ_TABLE', {QUERY_TABLE: 'USR01', DELIMITER: '|'}, (err, res) => {
if (err) return console.log(err)
console.log(res)
})
res.render('index');
});
module.exports = router;
错误:
Error: Client invoked RFC call with closed connection: id=1
at Client.invoke (/root/solo/node_modules/node-rfc/lib/wrapper/sapnwrfc-client.js:146:23)
at /root/solo/routes/index.js:19:9
at Layer.handle [as handle_request] (/root/solo/node_modules/express/lib/router/layer.js:95:5)
at next (/root/solo/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/root/solo/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/root/solo/node_modules/express/lib/router/layer.js:95:5)
at /root/solo/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/root/solo/node_modules/express/lib/router/index.js:335:12)
at next (/root/solo/node_modules/express/lib/router/index.js:275:10)
at Function.handle (/root/solo/node_modules/express/lib/router/index.js:174:3)
at router (/root/solo/node_modules/express/lib/router/index.js:47:12)
at Layer.handle [as handle_request] (/root/solo/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/root/solo/node_modules/express/lib/router/index.js:317:13)
at /root/solo/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/root/solo/node_modules/express/lib/router/index.js:335:12)
at next (/root/solo/node_modules/express/lib/router/index.js:275:10)
1条答案
按热度按时间hivapdat1#
您在客户端尚未连接时过早调用
invoke
。您正在使用的函数client.connect
接受回调作为参数,即当连接建立或失败时调用的函数。这意味着您必须在该函数内部调用RFC,如下所示:或者,如果你更喜欢async/await: