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: [], });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter