即使我传递参数carRentalId,也找不到找不到带有ID 404的客户的客户信息

时间:2018-08-14 10:06:27

标签: node.js mongodb express mongoose

let carRentalInfo = require('../models/carRental-model.js');

var infocar = {
            userName: req.body.userName, 
            password: req.body.password,
            email:req.body.email,
            firstName:req.body.firstName,
            lastName:req.body.lastName
        };
exports.findOne = (req, res) => {
    carRentalInfo.findById(req.params)
    .then(infocar => {
        console.log("yelo!!!",infocar)
        if(!infocar) {
            console.log("yelo!!!",infocar)
            return res.status(404).send({
                message: "customer's information not found for Customer's id " + req.params.carRentalId
            });            
        }
        res.send(infocar);
    }).catch(err => {
            if(err.kind === 'ObjectId') {
            return res.status(404).send({
                message: "Customer's information not found with Customer's id " + req.params.carRentalId
            });                
        }
        return res.status(500).send({
            message: "Error retrieving Customer's information with customer's id " + req.params.carRentalId
        });
    });
};

即使在传递carRentalId作为参数之后,也始终会出现404错误,并显示一条消息,该消息未找到带有客户ID的客户信息。我试图根据carRentalId填充客户的信息

1 个答案:

答案 0 :(得分:0)

仅当使用findById方法时,才需要传递ID。但是,您正在传递所有请求参数。要使其正常工作,您只需要按以下方式更改模型调用即可:

carRentalInfo.findById(req.params.id)