我正在构建一个应用程序,我希望管理员更新管理面板中显示的任何用户

问题描述 投票:0回答:1
我是初学者,陷入了这个问题。我正在构建一个应用程序,我希望管理员通过单击用户个人资料底部的更新按钮来更新管理面板中显示的任何用户。管理员只需替换显示的用户容器上的值即可更改姓名、性别等。我无法从管理面板更新用户,当我尝试更新用户时,我收到以下错误:

Cast to ObjectId failed for value ':id' (type string) at path '_id' for model 'User'
下面是我的index.js文件

import 'core-js' import 'regenerator-runtime/runtime' import { login, logout } from './login' import { updateSettings, createUser, updateUser } from './settings' const updateUserForm = document.querySelector('#update-user') if (updateUserForm) updateUserForm.addEventListener('click', e => { e.preventDefault() const form = new FormData() form.append('name', document.getElementById('name').value) form.append('email', document.getElementById('email').value) form.append('gender', document.getElementById('gender').value) form.append('number', document.getElementById('number').value) updateUser(form) })
下面是我的settings.js 文件

import axios from 'axios' import { showAlert } from './alerts' export const updateUser = async(data) => { try{ const res = await axios({ method: 'PATCH', url: (process.env.NODE_ENV === 'production') ? 'api/v1/users/:id' : 'http://127.0.0.1:5500/api/v1/users/:id', data }) if (res.data.status == 'success') { showAlert('success', 'User updated successsfully!') } location.reload(true) } catch(err) { showAlert('error', err.response.data.message) } }
下面是 userRoutes.js 文件

const express = require('express') const userController = require('./../controllers/userController') const authController = require('./../controllers/authController') const router = express.Router() router.use(authController.protect) router.use(authController.restrictTo('librarian')) router.route('/:id').get(userController.getUser).patch(userController.updateUser).delete(userController.deleteUser) module.exports = router
下面是userController.js

const factory = require('../controllers/handlerFactory') exports.getUser = factory.getOne(User) exports.updateUser = factory.updateOne(User) exports.deleteUser = factory.deleteOne(User)
下面是handlerFactory.js

const {Model} = require('mongoose') const catchAsync = require('../utils/catchAsync') const AppError = require('../utils/appError') exports.updateOne = Model => catchAsync(async(req, res, next) => { const doc = await Model.findByIdAndUpdate(req.params.id, req.body, { new: true, runValidators: true }) if (!doc) { return next(new AppError('No document found with that ID', 404)) } res.status(200).json({ status: 'success', data: { data: doc } }) })
下面是viewRoutes.js

const express = require('express') const authController = require('../controllers/authController') const viewController = require('../controllers/viewController') const router = express.Router() router.get('/students', authController.protect, viewController.getStudents) module.exports = router
下面是viewController.js

const User = require('../models/userModel') const catchAsync = require('../utils/catchAsync') exports.getStudents = catchAsync(async(req, res) => { const students = await User.find() res.status(200).render('students', { title: 'Students', students }) })
下面是学生.pug

extends account block user-account #user-account-content #display-user-containers each student in students if (student.role === 'student') form#get-user-form #user-container #user-container-header img#user-container-photo(src=`/img/users/${student.photo}`, alt=`${student.name} image`) input#name(type="text", value=`${student.name}`, name="name") #user-container-main #user-container-main-heading | Details label(for="email") | Email Address input#email.user-container-input(type="email", value=`${student.email}`, name="email") label(for="gender") | Gender input#gender.user-container-input(type="text", value=`${student.gender}`, name="gender") label(for="number") | Phone No. input#number.user-container-input(type="number", value=`${student.number}`, name="number") #user-container-footer button#update-user.upd-dlt-view-btn | Update button#delete-user.upd-dlt-view-btn | Delete button#view-books-issued.upd-dlt-view-btn | Issues
    
javascript mongodb mongoose dom pug
1个回答
0
投票
如果您可以通过在控制台中看到的确切错误消息来缩小发生此错误的确切行数,那么将很容易提供帮助。 我想它是从这里来的

exports.updateOne = Model => catchAsync(async(req, res, next) => { const doc = await Model.findByIdAndUpdate(req.params.id, req.body, {
检查 req.params.id 是否存在且 id 为 mongoose.Types.ObjectId。

© www.soinside.com 2019 - 2024. All rights reserved.