1

I am trying to get data from SAP system with Node.js app.

Please advise where to put async/await in my code or maybe use different approach?

I have tried bunch of different ways, but invoke still does not wait for connect to finish :(

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:

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)
Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
Yrk
  • 13
  • 2

1 Answers1

1

You're calling invoke too soon, when the client hasn't yet connected. The function client.connect you're using accepts a callback as a parameter, i.e. a function that's invoked when the connection is either established or failed. Which means that you have to invoke RFC inside that function, as follows:

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

Or, if you prefer 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);
    }
});
Dan Karbayev
  • 2,870
  • 2
  • 19
  • 28