使用node-rfc时出错:客户端使用关闭的连接调用RFC调用

zbsbpyhn  于 2023-06-05  发布在  Node.js
关注(0)|答案(1)|浏览(173)

我正在尝试使用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)
hivapdat

hivapdat1#

您在客户端尚未连接时过早调用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);
    }
});

相关问题