0

I cant execute a second request inside the request callback.

request({
    url: url,
    headers: {
        'auth-token': token
    },
    method: 'POST',
    json: true,
    body: data
}, (err, req, body) => {
    if (err) {
        console.log(err);
    } else {
        // Prosses data;

        // This is the second request.
        request({
            url: url2,
            headers; {
                'auth-token': token
            },
            method: 'POST',
            json: true,
            body: data2
        }, (err, req, body) => {
            if (err) {
                console.log(err);
                return;
            }

            //Process data.
        })
    }
})

The problem is that the second request is not executing. I am using nodemon to start the express server, but on the nodemon only the first request is receive on the express.

But the strange thing is that when I tried to call the method on the second time without closing the electron app, the second request is executed. And I can see it on the nodemon that the second request is executed first.

The output of the nodemon is like this.

POST /path/to/url 200 6.181 ms - 641 //-> this is the first execution. then there is nothing follows.
// Then I call the method again using the app. And the result is this.
POST /path/to/second/url 200 9.645 ms - 21
POST /path/to/url 200 21.628 - 641

It look like the /path/to/second/url is staying on somewhere nowhere and just send to the server if the method is called for the second time.

Please help, thanks.

Update: Added more info.

I have a folder could routes all the .js file is save there. then I am loading it using this on the my app.js

let rs = fs.readdirSync(path.join(process.cwd(), '/routes/'));
rs.forEach((file) => {
    if (file.indexOf('.js') !== -1) {
        let fn = '/' + file.replace(/\.[^/.]+$/, '');
        let pt = path.join(__dirname, './routes', fn);
        let req = require(pt);
        app.use(fn, req);
    }
});

Then on the routes files I have something similar like this.

router.post('url', (req, res) => {
  // here calling the controller. 
  // mostly just single line passing the request and result variable to the controller.
});

Actually that requests is called inside the ipc callback. I have a menuitems and on the click() event I just used the browserWindow.getFocusedWindow().webContents.send('ipc-name') then this will be triggered.

  ipc.on('ipc-name', () => {
       // The request is called here.
   }
zer09
  • 1,507
  • 2
  • 28
  • 48
  • I tried to do the same thing in a electron quick start project and it worked fine.I just added an event listener for click event to an button which triggered a function similar to what you posted.I used request module as you have.The only difference being it was a GET request.So to help you more information might be needed.Can you post the express router code of first url and also how you trigger the request function in electron? – Raghu Jan 07 '18 at 14:23
  • @Raghu I update the question added more info. – zer09 Jan 07 '18 at 14:59
  • I tried again but the issue is not reproduced.Here is the code which i tried, https://github.com/RaghuChandrasekaran/electron-quick-start. I faced a issue similar to this in electron but that was due to using `ipc.sendSync`. – Raghu Jan 07 '18 at 17:06
  • Assuming that this is due to your callback being waited on event loop of the process.Can you try the same but with trigger to be something else other than the menu click. – Raghu Jan 07 '18 at 17:15
  • @Raghu this is the reproducible sample project. https://github.com/zer09/request_test. I added the express server and the copy of your electron fork. – zer09 Jan 08 '18 at 03:28
  • I tried again with the the repo you shared but its still not reproducible.Sharing the SS, https://pasteboard.co/H27n1ut.png and https://cdn.pbrd.co/images/H27mSWh.png. My electron version is 1.7.8 & Node 8.9.4 and OS - Windows 10. – Raghu Jan 09 '18 at 11:39
  • hmm it look like it is isolated on fedora, tried it on Win10 on a VM and it works. – zer09 Jan 09 '18 at 15:23
  • Sorry for the late reply.Yes you are right.It got reproduced in Ubuntu.Better post this as an issue in github repo or the community forum to get a faster reply.I found one workaround which I have posted in the answer. – Raghu Jan 14 '18 at 15:46

1 Answers1

1

This does not solve the OP problem as the problem exists in Linux env but acts as an workaround.Instead of request module we have to make use of ClientRequest API in electron which is Event based and only makes the task much more difficult.But doesn't suffer from the issue faced in callback inside callback.

Example Code:

function triggerCall() {
    const request = net.request(`${root}/routes/get1`);
    request.on('response', (response) => {
        response.on('data', (chunk) => {
            console.log(`BODY: ${chunk}`)
        })
        response.on('end', () => {
          console.log('req 1 end');
            const request1 = net.request(`${root}/routes/get2`);
            request1.on('response', (response) => {
                response.on('data', (chunk) => {
                    console.log(`BODY: ${chunk}`)
                })
                response.on('end', () => {
                    console.log('req 2 end');
                })
            })
            request1.end();
        })
    })
    request.end();
};
Raghu
  • 909
  • 7
  • 23
  • Yup there is a way around, but this problem will boil up to request-promise. you can't promise chain the requests. – zer09 Jan 15 '18 at 04:34