使用 node-rfc 时出错:客户端调用关闭连接的 RFC 调用
Error when using node-rfc: Client invoked RFC call with closed connection
我正在尝试使用 Node.js 应用从 SAP 系统获取数据。
请告知在我的代码中将 async/await
放在哪里或者使用不同的方法?
我尝试了很多不同的方法,但调用仍然不等待连接完成:(
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)
您在客户端尚未连接时调用 invoke
太早了。您正在使用的函数 client.connect
接受回调作为参数,即在连接建立或失败时调用的函数。这意味着您必须在该函数内部调用 RFC,如下所示:
router.get('/', function(req, res, next) {
client.connect(err => {
if (err) {
return res.status(500).send(err);
}
client.invoke(
'RFC_READ_TABLE',
{ QUERY_TABLE: 'USR01', DELIMITER: '|' },
(err, result) => {
if (err) {
return res.status(500).send(err);
}
res.send(result);
}
)
});
});
或者,如果您更喜欢 async/await:
router.get('/', async function(req, res, next) {
try {
await client.open();
let result = await client.call(
'RFC_READ_TABLE',
{ QUERY_TABLE: 'USR01', DELIMITER: '|' }
);
res.send(result);
} catch (err) {
return res.status(500).send(err);
}
});
我正在尝试使用 Node.js 应用从 SAP 系统获取数据。
请告知在我的代码中将 async/await
放在哪里或者使用不同的方法?
我尝试了很多不同的方法,但调用仍然不等待连接完成:(
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)
您在客户端尚未连接时调用 invoke
太早了。您正在使用的函数 client.connect
接受回调作为参数,即在连接建立或失败时调用的函数。这意味着您必须在该函数内部调用 RFC,如下所示:
router.get('/', function(req, res, next) {
client.connect(err => {
if (err) {
return res.status(500).send(err);
}
client.invoke(
'RFC_READ_TABLE',
{ QUERY_TABLE: 'USR01', DELIMITER: '|' },
(err, result) => {
if (err) {
return res.status(500).send(err);
}
res.send(result);
}
)
});
});
或者,如果您更喜欢 async/await:
router.get('/', async function(req, res, next) {
try {
await client.open();
let result = await client.call(
'RFC_READ_TABLE',
{ QUERY_TABLE: 'USR01', DELIMITER: '|' }
);
res.send(result);
} catch (err) {
return res.status(500).send(err);
}
});