I cannot for the life of me figure out why this will not condense to one line of code. VS Code keeps telling me
':' expected. ts(1005)
I am trying to make a POST request to express and MongoDB with something that has 12 properties. It works just fine in Postman (and no VS Code warnings) when I write it out as such
year: req.body.year,
producer: req.body.producer,
director: req.body.director,
licenseStart: req.body.licenseStart,
licenseEnd: req.body.licenseEnd,
platform: req.body.platform,
requestorName: req.body.requestorName,
requestorEmail: req.body.requestorEmail,
requestorDepartment: req.body.requestorDepartment,
price: req.body.price,
notes: req.body.notes
However, when I try to replace the above with
const { title, year, producer, director, licenseStart, licenseEnd, platform, requestorName, requestorEmail, requestorDepartment, price, notes } = req.body
that is when VS Code gives me the above warning about the colon at the first curly brace after after "const". It also stops working in Postman. This should work right? I've even found tutorials doing the same thing. But can't find anyone else with the same issue. Here is the full function:
//Set purchases
//POST /api/purchases
//Private
//Sends request to add completely new purchase and sends a json response with that added purchase
const setPurchases = asyncHandler(async (req, res) => {
if (!req.body){
res.status(400)
throw new Error('Please fill in all the fields')
}
const purchase = await Purchase.create({
//fields in the Purchase Model/JSON Array/Schema in models/purchaseModels.js
//instantiates new Purchase
//const { title, year, producer, director, licenseStart, licenseEnd, platform, requestorName, requestorEmail, requestorDepartment, price, notes } = req.body
title: req.body.title,
year: req.body.year,
producer: req.body.producer,
director: req.body.director,
licenseStart: req.body.licenseStart,
licenseEnd: req.body.licenseEnd,
platform: req.body.platform,
requestorName: req.body.requestorName,
requestorEmail: req.body.requestorEmail,
requestorDepartment: req.body.requestorDepartment,
price: req.body.price,
notes: req.body.notes
})
res.status(200).json(purchase)
})