为了账号安全,请及时绑定邮箱和手机立即绑定

CastError:模型“客户”的路径“_id”处的值“客户”转换为 ObjectId 失败

CastError:模型“客户”的路径“_id”处的值“客户”转换为 ObjectId 失败

慕标琳琳 2023-04-14 16:23:28
我希望有人能帮我弄清楚我在这里做错了什么。我一直在搜索,我可以找到很多类似的问题,但没有一个我足够聪明来解决我的问题。我收到以下错误:CastError:模型“客户”的路径“_id”处的值“客户”转换为 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 :4371:21) 在 model.Query.Query.then (C:\Users\natha\Desktop\Coding\HAMMER\node_modules\mongoose\lib\query.js:4463:15) 在 processTicksAndRejections (internal/process/task_queues. js:93:5)还有一些让我感到困惑的事情,我的数据库中有另一个路由文件和另一个集合的控制器。它基本上是相同的代码,除了不是路由到 '/:Customers' 而是路由到 '/:Tickets' 并且它工作得很好。我看不到这段代码是如何工作的,而Customers代码却没有。
查看完整描述

3 回答

?
猛跑小猪

TA贡献1858条经验 获得超8个赞

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


app.use('/', customers);

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

app.use('/materials', materials)

app.use('/tickets', tickets)

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


查看完整回答
反对 回复 2023-04-14
?
森栏

TA贡献1810条经验 获得超5个赞

您在处理程序中有错误getCustomer。


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


getCustomer: async(req, res, next) => {

    try {

    const { customerID } = req.params;

    const customer= await Customer.findById(mongoose.Types.ObjectId(customerID));

    res.status(200).json(customer);  

    } catch(err) {

        next(err);

    }

  }


查看完整回答
反对 回复 2023-04-14
?
阿晨1998

TA贡献2037条经验 获得超6个赞

我赞成 OP 的解决方案。为了节省大量时间,我只想解释为什么这是一个问题以及其他人可以像我一样注意什么。所以在 OP server.js 中,'/' 出现在其他违反规则列出特定路由的路由之前第一的。意思是,如果在其他路由问题解决后移动了“/”。更重要的是,这个错误从何而来?当您调用任何其他路线时,您的应用程序首先调用“/”并使用客户的路线,同时将路线路径(即“/客户”、“/材料”、“/门票”)作为 id 参数传递,然后是 mongo正在尝试将客户、材料和门票作为客户集合中的 id。


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

app.use('/materials', materials)

app.use('/tickets', tickets)

app.use('/', customers);

这将解决 OP 的问题。


就我而言,这是问题所在:


router.get('/', getAllUser);

router.get('/:id', getUserById)

router.get('/facebook', facebookPassport)

router.get('/google', customersPassport);

所以在这里我得到这个错误Cast error for value "facebook"/"google" at path "_id" for model User因为最后两条路线中的字符串,“facebook”和“google”是特定的但永远不会到达,因为'/:id' 路由将接受 '/' 之后的任何值作为 id。当我这样重新排列时:


router.get('/', getAllUser);

router.get('/facebook', facebookPassport)

router.get('/google', customersPassport);

router.get('/:id', getUserById)

带走:在应用程序级别和路由文件中将特定路由移动到动态路由(带参数的路由)的顶部。


查看完整回答
反对 回复 2023-04-14
  • 3 回答
  • 0 关注
  • 107 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信