question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In response to the use of Promise return ctx.body, received '404 Not Found'.

See original GitHub issue

In response to the use of Promise return ctx.body, received ‘404 Not Found’. But my mongodb is showing that the data was written. This troubled me, look forward to help.

router.get('/api/login', function *(next) {
  let request = this.request;
  let response = this.response;
  let params = request.query;
  userInfo.set({
    document: {
      user: params.name,
      password: params.password
    }
  })
  .then(res => {
    response.body = {
      code: 0,
      data: {
        uid: res.uid,
      }
    };
  })
  .catch(err => {
    response.body = {
      code: 999999,
      error: err
    }
  })
  yield next;
})

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

4reactions
tjcommented, Jan 19, 2017

I’d personally suggest something like this:

try {
  this.body = yield userInfo.set({ ... })
} catch (error) {
  this.status = xxx
  this.body = { error }
}

Or omitting the try/catch portion and unifying your error-handling in middleware “upstream”.

0reactions
ytianzaishuicommented, May 31, 2018

just the last ctx.status = 200 ctx.body = {mes:‘fail’} do not works @tj

Read more comments on GitHub >

github_iconTop Results From Across the Web

koa.js returning 404 "Not Found" after signup, instead of the ...
I'm trying to get the jwt from body so i can make authenticated requests, but for some reason i keep on getting "Not...
Read more >
next generation web framework for node.js
Return response Content‑Length as a number when present, or deduce from ctx.body when possible, or undefined. response.body. Page 28. 6/27/2017.
Read more >
5.x API - Express.js
The function determines the file to serve by combining req.url with the provided root directory. When a file is not found, instead of...
Read more >
Best Practices for Node.js Error-handling - Toptal
Developers working with Node.js sometimes find themselves writing not-so-clean code while handling all sorts of errors. This article will introduce you to ...
Read more >
Resolving the JavaScript Promise Error "TypeError: Cannot ...
The response is then parsed into a JSON. This function is expected to return a Promise that should resolve to JSON data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found