Javascript CastError:value“的转换为ObjectId失败”;“客户”;在路径上“_id";对于“型号”;“客户”;

Javascript CastError:value“的转换为ObjectId失败”;“客户”;在路径上“_id";对于“型号”;“客户”;,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,我希望有人能帮我找出我做错了什么。我一直在寻找,我可以找到很多类似的问题,但没有什么是我足够聪明的解决我的问题。我得到以下错误: CastError:model“customer”的路径“\u id”处的值“customers”的转换为ObjectId失败 它以前是有效的,我设法打破了它,我解开了所有我认为我改变了的东西,我仍然得到了错误 这是我的模式: const Schema = mongoose.Schema; const CustomerSchema = new Schema({

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

CastError:model“customer”的路径“\u 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);
    }
  }
};

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);
   }
 } 
};
此外,以下是我得到的全部信息:

CastError:model“customer”的路径“\u id”处的值“customers”的转换为ObjectId失败 在model.Query.exec(C:\Users\natha\Desktop\Coding\HAMMER\node\u modules\mongoose\lib\Query.js:4371:21) 在model.Query.Query.then(C:\Users\natha\Desktop\Coding\HAMMER\node\u modules\mongoose\lib\Query.js:4463:15) 在ProcessTicks和Rejections(内部/流程/任务队列.js:93:5)

还有一些让我困惑的事情,我的数据库中有另一个集合的路由文件和控制器。它基本上是相同的代码,除了没有路由到“/:Customers”之外,它路由到“/:Tickets”,并且工作正常。我看不出这个代码是如何工作的,
客户
代码不是

路线:

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 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);
    }
  }
};

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

您在
getCustomer
处理程序中有一个错误

您不应该使用字符串id来查找客户,而是必须首先使用
mongoose.Types.ObjectId(string\u id)

getCustomer:async(请求、恢复、下一步)=>{
试一试{
const{customerID}=req.params;
const customer=wait customer.findById(mongoose.Types.ObjectId(customerID));
res.status(200).json(客户);
}捕捉(错误){
下一个(错误);
}
}

我发现了问题。我的server.js文件中有以下内容:

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

一旦我摆脱了
app.use(“/”,客户)一切正常。

谢谢您的回复!因此,我的
getCustomer
似乎可以正常工作,我按照您的建议进行了编辑,
getCustomer
仍然可以工作。我在运行
索引时遇到错误。奇怪的是,如果我在我的routes文件'.get(customerController.getCustomer)`中注释掉下面这行,那么
index
就可以正常工作了。但是当我取消注释“index”停止工作并且“getCustomer”工作正常时。@在索引处理程序中,您可以删除
{}
空对象条件,然后重试。删除空
{}
的结果与此相同。如果
.get(customerController.getCustomer)
被注释掉,索引处理程序就会工作。如果没有注释掉,则getCustomer处理程序可以工作。