hello , plz any one help me
i am getting duplicate key error while doing the POST action i tried several to correct them but I do not understand what is happening
Error MongoError: E11000 duplicate key error collection: timeformationBD.formations index: description_1 dup key: { : null }
this is my formation.route
var express = require('express');
var router = express.Router();
var Formation = require('../models/formations');
router.post('/', function(req, res){
var newFormation = {
name: req.body.name,
position : req.body.position,
department : req.body.department,
salary: req.body.salary
}
Formation.addFormation(newFormation,function(err,formation){
if(err) throw err;
res.json(formation);
});
})
module.exports = router
;
this is my formation.models
var mongoose = require('mongoose');
var FormationSchema = new mongoose.Schema({
name: String,
position : String,
department : String,
salary : String
})
var Formation = module.exports = mongoose.model('Formation', FormationSchema);
module.exports.addFormation = function(newFormation, callback){
Formation.create(newFormation, callback);
}
this is my app.js ( endpoint )
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const passport = require('passport');
const mongoose = require('mongoose');
const config = require('./config/database');
const User = require('./models/user');
const Formation = require('./models/formations');
mongoose.connect(config.database , { useNewUrlParser: true });
mongoose.connection.on('connected', () => {
console.log('connected to database... ' + config.database);
});
mongoose.connection.on('error', (err) => {
console.log('database error'+err);
});
/*mongoose.connect('mongodb://localhost:27017/hello', { useNewUrlParser: true });
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('we are connected!');
});*/
const app = express();
const users = require('./routes/users');
const formations = require('./routes/formations');
//port number
const port = 3001;
//cors middleware
app.use(cors({
origin: 'http://localhost:4200'
}));
//passwport middelware
app.use(passport.initialize());
app.use(passport.session());
require('./config/passport')(passport); //pour implémenter fichier passport.js
//set static folder
app.use(express.static(path.join(__dirname + '/public')));
//BodyParser middleware
app.use(express.json());
app.use(bodyParser.json());
app.use('/users', users);//hedhiya ki nekteb /users barka f url yemchili direct lel const users = require('./routes/users'); fichier hedhka
app.use('/formations', formations);
//c un route
//just meloul 7atineha bch ntastiw beha
//index route
app.get('/', function(req, res){
res.send('invalid endpoint');
});
//start Server
app.use(bodyParser.urlencoded({extended:true}));
app.listen(port, () => {
console.log('Server started on port' +port);
});
Aucun commentaire:
Enregistrer un commentaire