首页 > 解决方案 > CastError:模型“customer”的路径“_id”中的值“customers”转换为 ObjectId 失败

问题描述

我希望有人能帮我弄清楚我在这里做错了什么。我一直在寻找,我可以找到很多类似的问题,但没有什么我足够聪明来解决我的问题。我收到以下错误:

CastError:模型“customer”的路径“_id”中的值“customers”转换为 ObjectId 失败

它以前可以工作,但我设法打破了它,我撤消了我认为我改变的一切,但我仍然收到错误。

这是我的架构:

const Schema = mongoose.Schema;

const CustomerSchema = new Schema({

  custName: {
    type: String,
    required: true
  },
  custStreet: {
    type: String
  },
  custCity: {
    type: String
  },
  custState: {
    type: String
  },
  custZip: {
    type: String
  }
});

module.exports = Customer = mongoose.model('customer', CustomerSchema); 

我的路线:

const router = express.Router();

const customerController = require('../../controllers/customer')

const Customer = require('../../models/Customer');

router.route('/')
  .get(customerController.index)
  .post(customerController.newCustomer);

router.route('/:customerID')
  .get(customerController.getCustomer)

module.exports = router;

我的控制器:


module.exports = {

  index: async (req, res, next) => {
    try {
    const customers = await Customer.find({})
    res.status(200).json(customers);
  } catch(err) {
      next(err);
    }
  },

  newCustomer: async (req, res, next) => {
    try {
    const newCustomer = await Customer(req.body);
    const customer = await newCustomer.save();
    res.status(201).json(customer);
      } catch(err) {
          next(err);
      }
  }, 

  getCustomer: async(req, res, next) => {
    try {
    const { customerID } = req.params;
    const customer= await Customer.findById(customerID);
    res.status(200).json(customer);  
    } catch(err) {
        next(err);
    }
  }
};

此外,这是我收到的全部信息:

CastError:在model.Query.exec (C:\Users\natha\Desktop\Coding\HAMMER\node_modules\mongoose\lib\query.js 中,模型“customer”的路径“_id”的值“customers”转换为ObjectId 失败:4371:21) 在 model.Query.Query.then (C:\Users\natha\Desktop\Coding\HAMMER\node_modules\mongoose\lib\query.js:4463:15) 在 processTicksAndRejections (内部/进程/task_queues. js:93:5)

还有一些让我感到困惑的事情,我的数据库中有另一个路由文件和控制器用于另一个集合。它基本上是相同的代码,除了路由到'/:Customers'而不是路由到'/:Tickets'并且它工作得很好。我看不出这段代码是如何工作的,而且Customers代码不是。

路线:

const router = express.Router();

const ticketController = require('../../controllers/ticket');

router.route('/')
  .get(ticketController.index)
  .post(ticketController.newTicket);

router.route('/:ticketID')
  .get(ticketController.getTicket)
  .put(ticketController.replaceTicket)
  .patch(ticketController.updateTicket);

module.exports = router;

控制器:


module.exports = {

 index: async(req, res, next) => {
   try {
     const tickets = await Ticket.find({})
     res.status(200).json(tickets);
   } catch(err) {
     next(err);
   }
 },

 newTicket: async (req, res, next) => {
   try {
     const newTicket = await Ticket(req.body);
     const ticket = await newTicket.save();
     res.status(201).json(ticket);
   } catch(err) {
      next(err);
   }
 },

 getTicket: async(req, res, next) => {
  try {
  const { ticketID } = req.params;
  const ticket = await Ticket.findById(ticketID);
  res.status(200).json(ticket);
  } catch(err) {
      next(err);
  }
 }, 

 replaceTicket: async(req, res, next) =>{
   try {
     const { ticketID } = req.params;
     const newTicket = req.body;
     const result = await Ticket.findByIdAndUpdate(ticketID, newTicket);
     res.status(200).json({ Success: true });
   } catch(err) {
      next(err);
   }
 },

 updateTicket: async(req, res, next) =>{
   try {
     const { ticketID } = req.params;
     const newTicket = req.body;
     const result = await Ticket.findByIdAndUpdate(ticketID, newTicket);
     res.status(200).json({ Success: true });
   } catch(err) {
      next(err);
   }
 } 
};

我会很感激任何帮助!

谢谢

-N8

标签: javascriptnode.jsmongodbexpressmongoose

解决方案


我发现了问题。我的 server.js 文件中有这个:

app.use('/', customers);
app.use('/customers', customers);
app.use('/materials', materials)
app.use('/tickets', tickets)

一旦我摆脱了app.use('/', customers);一切都很好。


推荐阅读