2016-06-21 67 views
1

我在MongoDB中使用nodeJS。我想查詢數據庫,並添加populate像醫生說:Javascript store作爲mongoDB的變量逗號分隔字符串

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

的事情是,我需要使用'_creator', 'name'幾次。如何在我的文件開始時存儲這些信息,以便我可以多次使用它?

回答

2

隨着EMCAScript6,您可以使用spread operator ...

在文件的開頭使用數組作爲參數列表,可以使一個數組:

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

,然後引用它在你的代碼作爲:

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