亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 184 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號