javascript - 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
解决方案
我发现了问题。我的 server.js 文件中有这个:
app.use('/', customers);
app.use('/customers', customers);
app.use('/materials', materials)
app.use('/tickets', tickets)
一旦我摆脱了app.use('/', customers);
一切都很好。
推荐阅读
- python - [tensorflow]如何通过子类化模型类(tf.keras.Model)来包装一系列代码
- javascript - 如何在 Thymeleaf 中将 JS 变量添加到 DTO
- html - 如何在 Bootstrap 5 中的日期选择器输入之后放置日期图标?
- rabbitmq - 是否可以订阅rabbit mq消息队列以便在消息进入队列时获取消息而不使用while循环
- python - 有没有办法计算价值?
- python - 从最大数据的值在 Python 中创建一个列表
- javascript - 可以使用 Intersection Observer 来检查元素是否已经穿过视口的中间吗?
- android - Android : 如何构建一个可以通过 OTG/USB 将主机 android 设备上的 apk 安装到另一个连接的 android 设备上的应用程序
- java - Tapestry 中是否有将空字符串从服务器端转换为客户端空字符串的公共属性?
- firebase - Cloud Functions 从 Firestore 字段查询日期,这是一个字符串