第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

CastError:模型“”的路徑“_id”處的值“未定義”轉(zhuǎn)換為 ObjectId 失敗

CastError:模型“”的路徑“_id”處的值“未定義”轉(zhuǎn)換為 ObjectId 失敗

慕后森 2023-09-21 10:51:13
我正在嘗試使用 MERN 堆棧制作一個應(yīng)用程序。當(dāng)我嘗試將用戶添加到當(dāng)前數(shù)據(jù)庫時,它返回“CastError: Cast to ObjectId failed for value "undefined" at path "_id" for model "Project"我做錯了什么?”API.jsimport axios from "axios";export default {    updateUser: function (id, data) {    return axios.put("/api/user/" + id, data);  },}apiRoute.jsapp.put("/api/project/:id/add-user", async (req, res) => {    // Find the project that was created and update it with a user    // console.log("Hello")    try {      const dbProject = await db.Project.findOneAndUpdate({ _id: req.params.id }, {        // Append the User to the Project object        $push: { users: req.body.userId }      }, { new: true });      // Send the request back to the front end      res.send(dbProject)    } catch (error) {      console.log({ "PUT - Project Add User": error })      res.send(error)    }  });項目.js(模型)const mongoose = require('mongoose');const { Schema } = mongoose;const projectSchema = new Schema({  project_name: {    type: String,    unique: true  },  team_lead: String,  description: String,  tags: String,  location: String,  num_members: Number,  // image: String,  users: [    {      type: Schema.Types.ObjectId,      ref: "users"    }  ]});const Project = mongoose.model('Project', projectSchema);module.exports = Project;
查看完整描述

1 回答

?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗 獲得超13個贊

你的錯誤非常明顯出了什么問題。再讀一遍:

CastError:模型“Project”的路徑“_id”處的值“未定義”轉(zhuǎn)換為 ObjectId 失敗

undefined...路徑“_id”失敗...

這意味著您的req.params.id,因為它位于字段/路徑“_id”,所以未定義。檢查您是否確實向路線發(fā)送了一些值。

您從客戶端向您的路線發(fā)送了錯誤的參數(shù)

const dbProject = await db.Project.findOneAndUpdate({ _id: req.params.id }, {

    // Append the User to the Project object

    $push: { users: req.body.userId }

  }, { new: true });


查看完整回答
反對 回復(fù) 2023-09-21
  • 1 回答
  • 0 關(guān)注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號