Eugene
Eugene

Reputation: 11

Bad request sending GET request with id (ExpressJS)

Using MEAN.JS. Routes:

 app.route('/api/user/cards/:cardId')
   .get(card.read);
 app.param('cardId', card.cardByID);

cardByID:

exports.cardByID = function (req, res, next, id) {
      Card.findOne({id: id}).exec(function (err, card) {
       if (err) return next(err);
       if (!card) return res.status(404).send({message: 'Карты с таким ID не найдено'});
       req.card = card;
       next();
     });
    };

I used to use mongoose _id as param, but need to use other id (8 digits). It returns 404 status if id is mongoose id type (ex. 57ceda7ec10c15da7c53515f), but if id is just a number (ex. 13241234) it returns 400 status. What is the problem?

Upvotes: 1

Views: 205

Answers (1)

abdulbari
abdulbari

Reputation: 6232

As MongoDB says that the default unique identifier generated as the primary key _id for a document is an ObjectId.


And it's 12-byte binary value which is often represented as a 24 character hex string.

And whenever MongoDB get less than 24 characters it does not accept that as _id. That's why you are getting some error from MongoDB side.

Upvotes: 1

Related Questions