-1

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) 
})
Phil
  • 141,914
  • 21
  • 225
  • 223
Laz Austin
  • 21
  • 4
  • 1
    Why not simply use `Purchase.create(req.body)`? If you want to break references, use `{...req.body}` – Phil Apr 13 '22 at 01:29
  • 1
    "*This should work right? I've even found tutorials doing the same thing*" - nope. A `const` in the middle of an object literal (`Purchase.create({…})`) is a syntax error. – Bergi Apr 13 '22 at 01:50
  • @phil then VS Code gives me a ',' expected error under dot in req.body. I I also figured out how to do it using – Laz Austin Apr 13 '22 at 02:15
  • @LazAustin then you're doing something else wrong. Can't tell you what without seeing the code you're trying – Phil Apr 13 '22 at 02:18
  • @phil sorry I didn't mean to send my comment yet. VS Code gives me a ',' expected error under the period in req.body. I I also figured out how to do it using `const newPurchase = new Purchase(req.body); const savedPurpose = await newPurchase.save();` That also worked. But not I'm still very confused by the difference between .create and .save. Simply replacing everything under Purchase.create with `await Purchase.create({req.body})` did not work. I'm posting below in another comment the method I was trying to imitate. – Laz Austin Apr 13 '22 at 02:29
  • _"Purchase.create({req.body})"_... I think you need to look closer at my first comment. – Phil Apr 13 '22 at 02:30

1 Answers1

0

Based on your code, you are effectively changing:

someFunction({
  foo: bar.foo,
});

into:

someFunction({
   const { foo } = bar
});

This is not valid Javascript. The argument to Purchase.create is itself an object, and you cant write arbitrary code when building an object.

Evert
  • 83,661
  • 18
  • 106
  • 170
  • @Bergi So I've found two methods that work. The 12 lines starting with title: req.body.title above. But also `const newPurchase = new Purchase(req.body); const savedPurchase = await newPUrchase.save(); res.status(200).json(savedPurchase);` But right now I am also looking at a video by Traversy Media trying it the way I mentioned and it is working fine. His code: `const registerUser = asyncHandler(async (req, res) => {const {name, email, password} = req.body, ( – Laz Austin Apr 13 '22 at 02:35
  • @LazAustin `{ }` and `() => { }` are 2 very different things. The first specifies an object, and the second an arrow function. It's confusing they both use `{` and `}` but if the `{` and `}` is preceded by a `=>` it's a function. – Evert Apr 13 '22 at 03:29