sauceCtrl_createSauce

PHOTO EMBED

Wed Sep 08 2021 16:21:13 GMT+0000 (UTC)

Saved by @devdave

exports.createSauce = (req, res, next) => {
  // stocking data sent from front-end, as form-data, in a variable, parsed as a JS object
  // req.body = string 'sauce' that must be parsed
  const sauceObject = JSON.parse(req.body.sauce);

  // remove Id sent from front-end.
  // the sauce's Id is created by MongoDB
  delete sauceObject._id;

  // [1] 'new' => create new instance of Sauce model
  // [2] the Spread operator '...' => creates a new copy of all elements of req.body
  // https://geeklecode.com/loperateur-spread-en-javascript-va-vous-simplifier-la-vie/
  const sauce = new Sauce({
    ...sauceObject,
    // (a) ${req.protocol} = http or https
    // (b) ${req.get("host")} = target host server
    // (c) ${req.file.filename} = filename
    imageUrl: `${req.protocol}://${req.get("host")}/images/${
      req.file.filename
    }`,
    likes: 0,
    dislikes: 0,
    usersLiked: [],
    usersDisliked: [],
  });
content_copyCOPY