denislexic
denislexic

Reputation: 11342

Javascript Store as a variable comma separated strings for mongoDB

I'm using nodeJS with MongoDB. I'm trying to query the DB and add populate like the doc says:

Story
.findOne({ title: /timex/i })
.populate('_creator', 'name')

The thing is, I need to use '_creator', 'name' several times. How can I store this at the start of my file so that I can use it multiple times?

Upvotes: 1

Views: 210

Answers (1)

castletheperson
castletheperson

Reputation: 33466

With EMCAScript6, you can use an array as a parameter list using the spread operator ...

At the beginning of the file, you can make an array:

const params = ['_creator', 'name']

And then reference it throughout your code as:

Story
.findOne({ title: /timex/i })
.populate(...params)

Upvotes: 2

Related Questions