Node.JS Request Module Callback Not Firing

╄→гoц情女王★ 提交于 2019-12-24 02:10:53

问题


I'm running this code using the request module for node.js

var hsKey    = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var hsForm   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var hsHost   = "https://docs.google.com/"
var url = hsHost + "forms/d/" + hsForm + "/formResponse"

var form = {
    "entry.129401737": pointsAvg,
    "entry.2000749128": hiddenNeurons,
    "submit": "Submit",
    "formkey": hsKey
};

request.post({
    url: url,
    form: form
}, function (err, res, body) {
    console.log("Sent data");
});

I have tried running the above code just using standard Node.JS libraries, to no avail. The callback function is never fired and the request doesn't go through. I don't know why.


回答1:


I believe I've found the answer to my own problem. The issue seems to be that I'm not allocating any time in the Node.js event loop to allow the request to be executed.




回答2:


Have a look at this question:

your code should look something like

var hsKey    = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hsForm   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hsHost   = "https://docs.google.com/"
var url = hsHost + "forms/d/" + hsForm + "/formResponse"

var form = {
  "entry.129401737": pointsAvg,
  "entry.2000749128": hiddenNeurons,
  "submit": "Submit",
  "formkey": hsKey
};

request.post({
    url: url,
    form: form
}, function (response) {
      response.setEncoding('utf8');
      response.on('data', function(chunk){
          //do something with chunk
      });
});

The data event should get fired on receiving a response.

So if you read the docs for the request module at npm

request
    .get('http://google.com/img.png')
    .on('response', function(response) {
         console.log(response.statusCode) // 200 
         console.log(response.headers['content-type']) // 'image/png' 
     });

There is a response event that should get fired.




回答3:


I ran into this as well. I ended up creating a separate js file containing only the request, without the describe and it methods, and running it with 'mocha mynewbarebonesreq.js'. suddenly I could see that there was an exception being thrown and swallowed by mocha (with the standard reporter, spec).

I finally installed and enabled mocha_reporter which shows the exceptions

now it looks like this:

describe('CMSLogin', function () {
    it('should log in as user ' + JSON.stringify(USER_PASS), function (done) {
        request({
            url: "http://cms.lund.multiq.com:3000/api/CMSUsers/login",
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            json: false,
            body: JSON.stringify(USER_PASS)
        }, (err, res, body) => {
            var parsedBody = JSON.parse(body);
            this.token = parsedBody.id; 
            console.log(this.token)
            assert.equal(USER_PASS.userId, parsedBody.userId);
            assert.doesNotThrow(() => Date.parse(parsedBody.created));
            if (err) { done.fail(err); }

            done();
        });
    });
}


来源:https://stackoverflow.com/questions/35573641/node-js-request-module-callback-not-firing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!