Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Using SQL ORMs with Node.js Defining Models Go Further with Models

Seth Missiaen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seth Missiaen
Web Development Techdegree Graduate 21,652 Points

Trying to use destructuring inside the Movie.create() method

I added a helper function to accept the different pieces of data for each movie in order to make the code look more clean. I'm trying to utilize the destructuring shorthand like this:

const createMovie = (title, runtime, releaseDate, availableOnVHS) => {
  return Movie.create({
    { title },
    { runtime },
    { releaseDate },
    { availableOnVHS },
  })
}

This renders the following error:

/Users/.../sql-orms-with-node/app.js:8
    { title },
    ^

SyntaxError: Unexpected token '{'

When I run the code as follows, it works:

const createMovie = (title, runtime, releaseDate, availableOnVHS) => {
  return Movie.create({
    title: title,
    runtime: runtime,
    releaseDate: releaseDate,
    availableOnVHS: availableOnVHS,
  })
}

Is there a way I can use the destructuring assignment for the createMovie function?

Thanks!

1 Answer

Steven Parker
Steven Parker
230,995 Points

This isn't a case for restructuring, but you can use the shorthand for creating an object literal:

const createMovie = (title, runtime, releaseDate, availableOnVHS) =>
    Movie.create({title, runtime, releaseDate, availableOnVHS});
Seth Missiaen
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Seth Missiaen
Web Development Techdegree Graduate 21,652 Points

Okay thanks! So when creating an object literal, if I don't provide a key value pairβ€”just title for example, it will automatically be interpreted as title: title? I guess I thought that was an example of restructuring, my bad.