1

Relative beginner here, I'm having issues with express and async/await functions, with an unhandled promise rejection.

Here is my code on the client side, grabbing username and password from a page and sending it over.

async function signIn() {
    let username = document.getElementById('username').value
    let password = document.getElementById('password').value

    let url = '/api/login'
    url += '?username=' + username
    url += '&password=' + password

    const response = await fetch(url)
    console.log(response)
}

and here is my corresponding server function:

app.post('/api/login', function (req, res) {
    console.log("I'll log you in")
}

There's some code which does actually log in after that but that's all I need to create the error that I'm having.

This is the error I get in safari 11.03:

Unhandled Promise Rejection: TypeError: Type error

And this is the error in firefox 59.0.1:

TypeError: NetworkError when attempting to fetch resource.
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40
woofdinos
  • 419
  • 4
  • 14
  • Does your `fetch` return a Promise? It should for `await` to work https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function – knitevision Mar 30 '18 at 21:34
  • You're defining a 'POST' method with your express code, but I'm guessing that `fetch` uses a 'GET' by default. Maybe try changing `app.post(...` to `app.get(...` and see if you get results. Are you seeing the console "I'll log you in" on your server logs? – dmitrydwhite Mar 30 '18 at 22:21
  • I get the same result whether I use app.get or app.post, and no I’m not seeing “I’ll log you in” on my server logs – woofdinos Mar 31 '18 at 13:10

1 Answers1

0

For handling the promise rejection, you can put a try/catch block around the const response = await stuff:

let response;
try {
  response = await fetch(url)
catch(err) {
  // do stuff with error
}

(If you want a diff format for that: how use const in try catch block)

As for the error itself, do you know anything else about it? Can you share more of your code, or the server's error message? Let me know, I'll edit this answer when you post updated info.

JSilv
  • 1,035
  • 12
  • 26
  • Well here's what happens when I replace the `app.post('/api/login', function (req, res) { console.log("I'll log you in") }` with `app.post('/api/login', function (err) { if (err) { console.error(err) }` https://pastebin.com/a6yPAwHf (warning, 800 lines long) – woofdinos Mar 30 '18 at 21:57
  • The rest of the code doesn't change anything - even when I completely remove it, but I'll send you a github link if you want to have a go with the whole thing. – woofdinos Mar 30 '18 at 22:00
  • sure, that would be helpful – JSilv Mar 30 '18 at 22:01
  • ahh ya. if you want to add an error handler with your route, it has take four arguments `err, req, res, next`. what you logged was the entire request object. add this to `app.js` above your routes: `app.use((err, req, res, next) => { console.log(err); res.send(err); })` – JSilv Mar 30 '18 at 22:03