8

I'm new to Nodejs. How to export multiple objects? For example

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

I want to use Page model something like:

Page = require('./models').Page

I want to use Sequelize object something like:

Sequelize = require('./models').Sequelize

Thank you for advice :D

UPDATED: Solved following way:

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)
  @

but I have an another problem. How to assign models dynamically?

UPDATED Solved second problem following way:

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')
  @

1 Answer 1

9

Just wrap them in an object literal. In pure JS (without CoffeeScript) that would be:

module.exports = {
    Page: {}, // or whatever you want to assign it to
    Serialize: {} // again, set it to what you like
};

In coffeescript you use indents, except when you want to make an empty object:

module.exports =
  Page: {},
  Serialize: {}
Sign up to request clarification or add additional context in comments.

5 Comments

= should be : in the object literal :)
What do you mean by "How to assign models dynamically"?
Sorry for poor English. For example. I have 3 models in my /server/models folder. As you can see I'm loop through each models. Right now I'm just assign each models to @Page variable. How to make it dynamically.
How to assign each models to different variable like @Page?
You can use a string for the property name in pure JS. So instead of Page: {} you can use "SomeName" : {} and you can change "SomeName" to whatever you like. I don't know how this can be done in CoffeeScript.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.