Guest User

Untitled

a guest
Mar 22nd, 2019
77
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const app = express();
  4. const Enseignants = require('./models/enseignants');
  5. app.use(bodyParser.urlencoded({ extended: true }));
  6. const mongoose = require('mongoose');
  7. mongoose.connect('mongodb+srv://admin:admin@projetbobo-wcoxy.mongodb.net/test?retryWrites=true')
  8. .then(() => {
  9. console.log('Successfully connected to MongoDB Atlas!');
  10. })
  11. .catch((error) => {
  12. console.log('Unable to connect to MongoDB Atlas!');
  13. console.error(error);
  14. });
  15.  
  16. app.post('/ajoutens', (req, res) => {
  17. var msg;
  18. var x = 0;
  19. var findQuery = Enseignants.find({
  20. email: req.body.Email
  21. });
  22. var countQuery = findQuery.count();
  23.  
  24.  
  25. countQuery.exec(function (e, count) {
  26. if (count == 0) {
  27.  
  28.  
  29.  
  30. const enseignants = new Enseignants({
  31.  
  32.  
  33. nom: req.body.Nom,
  34. prenom: req.body.Prenom,
  35. email: req.body.Email,
  36. pass: req.body.pass,
  37. rang: req.body.rang,
  38. mat: req.body.mat,
  39. num: Number(req.body.tel),
  40. photo: req.body.photo
  41. });
  42. enseignants.save().then(
  43.  
  44. () => {
  45. res.status(201).json({
  46. message: 'Post saved successfully!',
  47.  
  48. });
  49. console.log('succ');
  50.  
  51. }
  52. ).catch(
  53. (error) => {
  54. res.status(400).json({
  55.  
  56. error: error
  57. });
  58. console.log('ops');
  59.  
  60. }
  61. );
  62.  
  63. }
  64. else {
  65. res.status(201).json({
  66. message: 'exist',
  67.  
  68. });
  69. console.log("exist deja");
  70.  
  71.  
  72. }
  73.  
  74. })
  75.  
  76.  
  77. });
  78.  
  79.  
  80.  
  81.  
  82. module.exports = app;
RAW Paste Data