Reputation: 2674
I have a collection of users:
db.users.find()
{ "_id" : ObjectId("56d9f3435ce78127510332ea"), "index" : 1, "isActive" : false, "name" : "Noble", "surname" : "Downs", "email" : "[email protected]", "phone" : "+1 (812) 412-3775", "address" : "357 River Street, Chelsea, District Of Columbia, 5938" }
{ "_id" : ObjectId("56d9f3435ce78127510332eb"), "index" : 0, "isActive" : false, "name" : "Moore", "surname" : "Vinson", "email" : "[email protected]", "phone" : "+1 (902) 511-2314", "address" : "433 Sullivan Street, Twilight, Maine, 4931" }
{ "_id" : ObjectId("56d9f3435ce78127510332ec"), "index" : 4, "isActive" : false, "name" : "Madge", "surname" : "Garza", "email" : "[email protected]", "phone" : "+1 (828) 425-3938", "address" : "256 Bowery Street, Chesapeake, Wyoming, 1688" }
{ "_id" : "56bc57c4ea0ba50642eb0418", "index" : 10, "isActive" : true, "name" : "Maritza", "surname" : "Foster", "email" : "[email protected]", "phone" : "+1 (884) 416-2351", "address" : "556 Conway Street, Ernstville, Pennsylvania, 134" }
Please note that the last one does not have Object_id, just for my tests. I'm using Koa2 and using async /await though I think it doesn't affect. My route is mounted on /api/users and is like this:
import User from '../models/user'
var router = require('koa-router')();
var ObjectId = require('mongoose').Types.ObjectId;
router
.get('/', async ctx => ctx.body = await User.find({}))
.get('/:id',
async (ctx) => {
try {
let id = ObjectId(ctx.params.id);
const user = await User.findById(id)
if (!user) {
ctx.throw(404)
}
ctx.body = user
} catch (err) {
if (err === 404 || err.name === 'CastError') {
ctx.throw(404)
}
ctx.throw(500)
}
})
When I load http://localhost:3000/api/users all my users are shown.
When I load http://localhost:3000/api/users/56d9f3435ce78127510332ea only that user is shown.
However.... when I load http://localhost:3000/api/users/56bc57c4ea0ba50642eb0418 I got Internal Server error.
If I load http://localhost:3000/api/users/whatever I also get Internal Server Error
So my questions: findById is always expecting an Object_id? If this Object_id is no longer in the collections shouldn't it return 404?
What happens if I import my data with my own id? Can't I use findById method? Even if I commented this line? let id = ObjectId(ctx.params.id);
Shouldn't a I get a 404 error?
If I change ctx.throw (500) for ctx.body = err I always get {} in the browser. Maybe that's the reason, the err is empty. But why?
Upvotes: 0
Views: 367
Reputation: 174
if (!user) {
ctx.throw(404)
}
will throw an exception and it will be caught by your try/catch statement:
} catch (err) {
if (err === 404 || err.name === 'CastError') {
ctx.throw(404)
}
ctx.throw(500)
}
but err != 404 so it will skip the if statement in try/catch and instead do ctx.throw(500)
to fix it you can do following:
} catch (err) {
if (err.message === 'Not Found' || err.name === 'CastError') {
ctx.throw(404)
}
ctx.throw(500)
}
Upvotes: 0