Привет всем, я пытался выяснить, почему мой код не позволяет мне добавлять новый объект Developer в мою базу данных MongoDB.Всякий раз, когда я пытаюсь, я просто получаю «ValidationError», и я не могу понять, почему.
Схема Developer.js
const mongoose = require('mongoose');
let developerSchema = mongoose.Schema({
_id: mongoose.Schema.Types.ObjectId,
name: {
firstName : {
type: String,
required: true
},
lastName : String
},
level: {
type: String,
uppercase: true,
enum: ["BEGINNER", "EXPERT"],
required: true
},
address: {
state: String,
suburb: String,
street: String,
unit: String
}
});
module.exports = mongoose.model('Developer', developerSchema);
Код для вставки разработчика в основной файл server.js
let addfirstName = req.body.devFirstName;
let addlastName = req.body.devLastName;
let lvl = req.body.devLevelStatus;
let addState = req.body.addressState;
let addSuburb = req.body.addressSuburb;
let addStreet = req.body.addressStreet;
let addUnit = req.body.addressUnit;
mongoose.connect('mongodb://localhost:27017/DevelopersWeek7', function (err) {
if (err) {
console.log('Error in Mongoose connection');
throw err;
}
console.log('Successfully connected');
let addDeveloper = new Developer({
_id: new mongoose.Types.ObjectId(),
name: {
firstName : addfirstName,
lastName : addlastName
},
level: lvl,
address: {
state: addState,
suburb: addSuburb,
street: addStreet,
unit: addUnit
}
});
addDeveloper.save(addDeveloper, function(err){
if (err) throw err;
console.log('Developer successfully Added to DB');
});
});
Переменные в начале блока кода взяты со страницы HTML.
HTML СТРАНИЦА, ГДЕ req.body получает его значение
<html>
<link rel = "stylesheet" src = "colours.css">
<head>
<h1>
Add a new Developer
</h1>
</head>
<body>
<form name = "/insertDeveloper" method = "POST">
Developer First Name <br> <input type = text name = "devFirstName"><br>
Developer Last Name <br> <input type = text name = "devLastName"><br>
Level: <br> <select name = "devLevelSelect">
<option value = "BEGINNER"> Beginner </option>
<option value = "EXPERT"> Expert </option>
</select>
<br>
Address: State <br> <input type = text name = "addressState"><br>
Address: Suburb <br> <input type = text name = "addressSuburb"><br>
Address: Street <br> <input type = text name = "addressStreet"><br>
Address: Unit <br> <input type = text name = "addressUnit"><br>
<button href = "/">
Submit
</button>
</form>
</body>