6
我是新來的Nodejs。如何導出多個對象?例如Nodejs:如何導出多個對象?
fs = require("fs")
Sequelize = require 'sequelize'
module.exports = (config)->
sequelize = new Sequelize(
config.database.name
config.database.username
config.database.password
config.database.options
)
fs.readdirSync(config.root+'/server/models/').forEach (file)->
module.exports['Page'] = sequelize.import(config.root + '/server/models/' + file) # --> export this model
sequelize # --> export this object
我想用Page
模式是這樣的:
Page = require('./models').Page
我想用Sequelize
對象類似:
Sequelize = require('./models').Sequelize
謝謝你的忠告:d
已更新: 解決了以下方法:
fs = require("fs")
Sequelize = require 'sequelize'
module.exports = (config)->
@Sequelize = new Sequelize(
config.database.name
config.database.username
config.database.password
config.database.options
)
fs.readdirSync(config.root+'/server/models/').forEach (file)->
@Page = sequelize.import(config.root + '/server/models/' + file)
@
,但我有一個另一個問題。如何動態分配模型?
修訂 以下方式解決第二個問題:
fs = require("fs")
Sequelize = require 'sequelize'
module.exports = (config)->
@Sequelize = new Sequelize(
config.database.name
config.database.username
config.database.password
config.database.options
)
# @Page = sequelize.import(config.root + '/server/models/page')
fs.readdirSync(config.root+'/server/models/').forEach (file)->
str = file.replace('.coffee', '')
model = str.charAt(0).toUpperCase() + str.slice(1)
@[model] = @Sequelize.import(config.root + '/server/models/' + file) if ~file.indexOf('.coffee')
@
'='應該是':'在對象字面:) – robertklep
當然。謝謝,@robertklep – Slavo
謝謝你的回覆。查看我的更新:D – Zeck