【问题标题】:Error when using node-rfc: Client invoked RFC call with closed connection使用 node-rfc 时出错:客户端调用 RFC 调用并关闭连接
【发布时间】:2020-04-18 22:42:14
【问题描述】:

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

【问题讨论】:

    标签: node.js sap saprfc


    【解决方案1】:

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

    或者,如果您更喜欢异步/等待:

    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);
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-25
      • 2017-05-16
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多