Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //tworzenie
  2. router.post('/', (req, res, next) => {
  3.  
  4.     Photo.findById(req.body.photoId)
  5.         .then(photo => {
  6.             if (!photo) {
  7.                 return res.status(404).json({
  8.                     message: "Photo not found"
  9.                 });
  10.             }
  11.             const user = new User({
  12.                 _id: new mongoose.Types.ObjectId(),
  13.                 name: req.body.name,
  14.                 email: req.body.email,
  15.                 password: req.body.password,
  16.                 photo: req.body.photoId, //wpisuje photoId w Body w postmanie
  17.  
  18.             });
  19.             return user.save();
  20.         })
  21.         .then(result => {
  22.             console.log(result);
  23.             res.status(201).json({
  24.                 message: 'Post user',
  25.                 createdUser: {
  26.                     _id: result._id,
  27.                     name: result.name,
  28.                     email: result.email,
  29.                     password: result.password,
  30.                     photo: result.photo,
  31.                 },
  32.                 request: {
  33.                     type: "GET",
  34.                     url: 'http://localhost:3000/user/' + result._id
  35.                 }
  36.             });
  37.         })
  38.         .catch(err => {
  39.             console.log(err);
  40.             res.status(500).json({
  41.                 error: err
  42.             });
  43.         });
  44. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement