【问题标题】:Unable to send request from request callback无法从请求回调发送请求
【发布时间】:2018-01-14 23:49:29
【问题描述】:

我无法在请求回调中执行第二个请求。

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

问题是第二个请求没有执行。 我正在使用 nodemon 启动express server,但在nodemon 上,express 上仅接收第一个请求。

但奇怪的是,当我第二次尝试在不关闭electron 应用程序的情况下调用该方法时,会执行second request。我可以在nodemon 上看到第二个请求首先执行。

nodemon的输出是这样的。

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

看起来/path/to/second/url 不知道在某个地方,如果第二次调用该方法,它只会发送到服务器。

请帮忙,谢谢。

更新:添加了更多信息。

我有一个文件夹可以routes 所有.js 文件都保存在那里。 然后我在我的 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);
    }
});

然后在路线文件上我有类似的东西。

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

实际上,请求是在 ipc 回调中调用的。我有一个菜单项,在 click() 事件中我只使用了 browserWindow.getFocusedWindow().webContents.send('ipc-name') 然后这将被触发。

  ipc.on('ipc-name', () => {
       // The request is called here.
   }

【问题讨论】:

  • 我尝试在电子快速启动项目中做同样的事情,它工作得很好。我只是为点击事件添加了一个事件监听器到一个按钮,它触发了一个类似于你发布的功能。我用过和你一样的请求模块。唯一的区别是它是一个 GET 请求。所以可能需要更多信息来帮助你。你能发布第一个 url 的快速路由器代码以及如何在电子中触发请求功能吗?
  • @Raghu 我更新了问题添加了更多信息。
  • 我又试了一次,但问题没有重现。这是我试过的代码,github.com/RaghuChandrasekaran/electron-quick-start。我在电子中遇到了与此类似的问题,但这是由于使用ipc.sendSync
  • 假设这是由于您的回调正在等待进程的事件循环。您可以尝试相同的方法,但触发器是菜单单击以外的其他内容。
  • @Raghu 这是可重现的示例项目。 github.com/zer09/request_test。我添加了快递服务器和你的电子叉的副本。

标签: node.js express electron requestjs


【解决方案1】:

这并不能解决 OP 问题,因为该问题存在于 Linux 环境中,但可以作为一种解决方法。我们必须在电子中使用基于事件的 ClientRequest API 而不是请求模块,并且只会使任务更多困难。但不会遇到回调内部回调面临的问题。

示例代码:

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

【讨论】:

  • 是的,有办法解决,但这个问题将归结为请求承诺。你不能承诺链接请求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-30
  • 2011-07-17
  • 1970-01-01
  • 1970-01-01
  • 2013-05-21
  • 1970-01-01
相关资源
最近更新 更多