1

I have a code similar to this:

    // src/getSessionId.js
function callback(error, response) {
  // some code
  return sessionId; // typeof string
}

async function getSessionId(url = DEFAULT_URL) {
  const sessionId = await request.post(url, callback);
  console.log(sessionId); // logs the expected sessionId
  return sessionId;
}

// src/scrape.js
async function scrape() {
  const sessionId = await getSessionId('http://www.someUrl.com/');
  // expected: sessionId === getSessionId is true => the one I logged before
  // actual: sessionId is the response body => <html> ... <body> ... </body> ... </html>
}

I want the expected sessionId, and not the response html body. Why does this not work as I expected it?

The code can be found here: https://bitbucket.org/ytNeskews/handelsregister-scraper/src

Neskews
  • 764
  • 1
  • 10
  • 23
  • 1
    Snippets are for **runnable-in-browser** code. Yours isn't, so I've made it just a code block. – T.J. Crowder Jun 13 '18 at 17:26
  • 3
    If the `console.log(sessionId);` shows the session ID, the result of `await getSessionId` **will** be that same value given the code above. Please update your question with a [mcve] demonstrating the problem that we can copy and paste into a file and run with Node.js. (Also please give the details of your Node.js version and the versions of any scripts the code relies on.) – T.J. Crowder Jun 13 '18 at 17:28
  • *logs the expected sessionId* and *actual: sessionId is the response body* - that's very unlikely. Just because things should work as you expect, and that they don't work like that suggests that your case differs from the description. Consider providing a way how the problem can be replicated - a repo, etc. See https://stackoverflow.com/help/mcve – Estus Flask Jun 13 '18 at 17:29
  • It may be because of the async . Async function doesnt wait for the await method so before even getting the value of the post request to the url or getting callback the `getSessionId`method is returning which is not expected output . You can use promise instead of async or generators . I will recommend to use promise or better wrap the return statment of thr getsessionid method in the post callback – Aniketh Saha Jun 13 '18 at 17:30
  • You are using request to make a post. That method is likely returning an object with a body, which is totally expected. The sessionId will be located on the body, so why not, in your getSessionId, return 'response.body.sessionId' or whatever it happens to be? *assuming you assign const response to await request.post.... – Devin Fields Jun 13 '18 at 17:31
  • As others have said, we could really use your actual code to better understand what you are trying to accomplish. You may even be querying a website, which is returning you it's actual index file. – Devin Fields Jun 13 '18 at 17:35
  • I like how the discussion is happening on all 'what if' scenarios. Programmers at work :D – kiddorails Jun 13 '18 at 17:46
  • Thanks for your answers. I created a repo for this. It should be available here: https://bitbucket.org/ytNeskews/handelsregister-scraper/src – Neskews Jun 13 '18 at 17:48
  • Note that I tried to use getSessionId().then(...); which did not work either. – Neskews Jun 13 '18 at 17:53
  • What does the url './defaults' refer to? Are you making a post request to a directory? – Devin Fields Jun 13 '18 at 18:01
  • @DevinFields it is a file => src/defaults.js Currently I am using it just for the web address I want request data from. – Neskews Jun 13 '18 at 18:29

0 Answers0