vendredi 27 juillet 2018

when i did res.json (newschema), the properties are not displayed anymore on postman

hello , plz i need a help , when i did res.json (newschema) on post action , the properties are not displayed anymore on postman and also in mongodb please is there a solution

this is formations.routes

const express = require('express');
const router = express.Router();
const Formation = require('../models/formations');
const mongoose = require('mongoose');


const config = require('../config/database');





router.get('/', function(req, res){
//Formation.getFormations(function(err, formations){
   // if(err)throw err;
  //  res.json(formations);
//});
Formation.find({})
.exec(function(err, Formations){
    if(err){
        console.log('error'); 
    }else{
        res.json(Formations);
    }
})
});



router.post('/', function(req, res){
    console.log('post a formations');
    var newFormation = new Formation();
    newFormation.title = req.body.title;
    newFormation.url = req.body.url;
    newFormation.description = req.body.description;
    newFormation.save(function(err, newFormation){
        if(err){
            console.log('error insertion');
        } else {
            res.json(newFormation);
            //res.json({success: true, msg:'user registred'});
        }
    });
});

this is formation.models

const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const config = require('../config/database');


const FormationSchema = mongoose.Schema ({
    title:  String,
    url: String,
    description:  String



});
module.exports = mongoose.model('Formation', FormationSchema, 'Formations');

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);
 });

and this is how i make a post action in postman ethis is how i make a post action in postman




Aucun commentaire:

Enregistrer un commentaire