您好我正在用express创建一个nodejs api并使用mongoDB。现在我有以下文件,app.js文件
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
app.use(bodyParser.json());
Doctor = require('./Models/doctor');
Pacient = require('./Models/pacient');
// connect to mongoose
mongoose.connect('mongodb://localhost/tfmehealth');
var db = mongoose.connection;
app.get('/', function(req,res){
res.send('Please use /api/endpoint_name');
});
app.post('/api/signup',function(req,res){
console.log(req.body);
var pacientR = req.body;
Pacient.addPacient(pacientR, function(err, pacient){
if(err) {
throw err;
}
res.json(pacient)
});
});
doctor.js文件
var mongoose = require('mongoose');
//Doctor Schema
var doctorSchema = mongoose.Schema({
name: {
type:String
},
lastname1: {
type:String
},
lastname2: {
type:String
},
email: {
type:String
},
password: {
type:String
},
specialty: {
type:String
},
dateOfBirth: {
type:String
}
});
var Doctor = module.exports = mongoose.model('Doctor', doctorSchema);
//Get doctors
module.exports.getDoctors = function(callback, limit) {
Doctor.find().exec(callback);
}
//Get doctor by email
module.exports.getDoctorByEmail = function(emailid, callback) {
Doctor.findOne(emailid).exec(callback);
}
//Get doctor by id
module.exports.getDoctorById = function(id, callback) {
Doctor.findById(id).exec(callback);
}
//store doctor
module.exports.addDoctor = function(doctor, callback) {
Doctor.create(doctor, callback);
}
//update doctor
module.exports.updateDoctor = function(id, doctor, options, callback) {
var query = {_id:id};
var update = {
name: doctor.name,
lastname1: doctor.lastname1,
lastname2: doctor.lastname2,
email: doctor.email,
//password: doctor.password,
specialty: doctor.specialty,
dateOfBirth: doctor.dateOfBirth
}
Doctor.findOneAndUpdate(query, update, options, callback);
}
和pacient.js文件
var mongoose = require('mongoose');
//Pacient Schema
var pacientSchema = mongoose.Schema({
name: {
type:String
},
lastname1: {
type:String
},
lastname2: {
type:String
},
email: {
type:String
},
password: {
type:String
},
medicines: {
type:String
},
allergies: {
type:String
},
surgeries: {
type:String
},
dateOfBirth: {
type:String
},
weight: {
type:String
},
height: {
type:String
},
diseases: {
type:String
},
comments: {
type:String
}
});
var Pacient = module.exports = mongoose.model('Pacient', pacientSchema);
module.exports.getPacients = function(allback) {
Pacient.find.exec(callback);
}
//Get pacient by email
module.exports.getPacientByEmail = function(emailid, callback) {
Pacient.findOne(emailid).exec(callback);
}
//Get pacient by id
module.exports.getPacientById = function(id, callback) {
Pacient.findById(id).exec(callback);
}
//store pacient
module.exports.addPacient = function(pacient, callback) {
Pacient.create(pacient, callback);
}
//update pacient
module.exports.updatePacient = function(id, pacient, options, callback) {
var query = {_id:id};
var update = {
name: pacient.name,
lastname1: pacient.lastname1,
lastname2: pacient.lastname2,
email: pacient.email,
password: pacient.password,
medicines: pacient.medicines,
allergies: pacient.allergies,
surgeries: pacient.surgeries,
dateOfBirth: pacient.dateOfBirth,
weight: pacient.weight,
height: pacient.height,
diseases: pacient.diseases,
comments: pacient.comments
}
Pacient.findOneAndUpdate(query, update, options, callback);
}
doctor.js文件中的函数工作得很好,但是pacient.js文件中的所有函数总是说错误TypeError:X不是函数,我不知道该Pacient缺少什么对象,我看到它与Doctor对象相同,但它不起作用,任何想法??
由于