我正在开发一个平均堆栈应用程序,并且该应用程序我开发了一个模块并执行CURD操作,每当我更新记录时间获取值并更改表格值时,我单击更新该时间而不更新记录并给我错误< / p>
TypeError: Cannot read property 'id' of undefined
1)这是我的controller.js文件:
import mongoose from 'mongoose';
export const updatePatients = (req,res) => {
Patients.findOneAndUpdate({ _id:req.body.id }, req.body, { new:true }, (err,patients) => {
if(err){
return res.json({'success':false,'message':'Some Error','error':err});
}
console.log(patients);
return res.json({'success':true,'message':'Patients Updated Successfully',patients});
})
}
2)这是我的route.js文件
import express from 'express';
//import controller file
import * as patientsController from '../controllers/patients.server.controller';
// get an instance of express router
const router = express.Router();
router.route('/')
.get(patientsController.getPatients)
.post(patientsController.addPatient)
.put(patientsController.updatePatients);
router.route('/:id')
.get(patientsController.listPatient)
.delete(patientsController.deletePatients);
export default router;
3)这是我的modal.js文件
import mongoose from 'mongoose';
var Schema = mongoose.Schema({
createdAt:{
type: Date,
default: Date.now
},
titleofuser: {
type: String,
default: ''
},
fname: {
type: String,
default: '',
required: 'Please fill Patient First name',
trim: true
},
mname: {
type: String,
default: ''
},
lname: {
type: String,
default: '',
required: 'Please fill Patient Last name',
trim: true
},
knownperson: {
type: String,
default: ''
},
gender: {
type: String,
default: ''
},
dateofbirth:{
type: Date,
default: '',
required: 'Please Select Patient Date Of Birth',
trim: true
},
nhs_no: {
type: Number,
default: ''
},
profile: {
type: String,
default: ''
},
care_home: {
type: String,
default: '',
required: 'Please Select Patient Care Home Name',
trim: true
},
store_name:{
type: String,
default: '',
required: 'Please Select Patient Care Store',
trim: true
},
room_no:{
type: Number,
default: ''
},
addmission:{
type: Date,
default: '',
},
on_warfarin:{
type: Boolean,
default: false,
},
on_insulin:{
type: Boolean,
default: false,
},
on_covert_meds:{
type: Boolean,
default: false
},
service_notes :{
type: String,
default: '',
},
keyworker:{
type: String,
default: '',
},
doctor:{
type:String,
default:'',
required:'Please Select Doctor Name',
trim:true,
},
nurse:{
type:String,
default:''
},
pnurse:{
type:String,
default:''
},
consultant:{
type:String,
default:''
}
});
export default mongoose.model('Patients', Schema);
4)这是我的主要app.js文件
import express from 'express';
import path from 'path';
import bodyParser from 'body-parser';
import logger from 'morgan';
import mongoose from 'mongoose';
import SourceMapSupport from 'source-map-support';
// import routes
import patientsRoutes from './routes/patients.server.route';
// define our app using express
const app = express();
// allow-cors
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// allow preflight
if (req.method === 'OPTIONS') {
res.sendStatus(200);
} else {
next();
}
});
// express-busboy to parse multipart/form-data and x-www-form-urlencoded both
// bb.extend(app);
// configure app
app.use(logger('dev'));
app.use(express.static(path.join(__dirname, 'public')));
// set the port
const port = process.env.PORT || 3001;
// connect to database
mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost/NeviMeds', {
useMongoClient: true,
});
// add Source Map Support
SourceMapSupport.install();
app.use('/apiforpatients' , patientsRoutes);
app.get('/', (req,res) => {
return res.end('Api working');
})
// catch 404
app.use((req, res, next) => {
res.status(404).send('<h2 align=center>Page Not Found!</h2>');
});
// start the server
app.listen(port,() => {
console.log(`App Server Listening at ${port}`);
});
请输入我如何更改更新功能