Shamoon
Shamoon

Reputation: 43501

What's the proper way to define a `model` in Node.js using MongoDB?

mongoose = require 'mongoose'

class Locations
  constructor: @(host, port) ->
    @db = new mongoose 'locations', new Server(host, port, {auto_reconnect: true}, {})
    this.db.open ->
    null

  getAll: (callback) ->
    @db.collection 'locations', (err, locations_collection) ->
      if err?
        callback err
      else
        callback null, locations_collection
      null

exports.Locations = Locations

I have that in a file called locations.coffee and in my app.js, I have

locationsModel = require '../models/locations'
locationModel = new locationsModel 'localhost', 27017  

But apparently it never gets instantiated because I get

node.js:134
        throw e; // process.nextTick error, or 'error' event on first tick
        ^
ReferenceError: host is not defined

Upvotes: 3

Views: 635

Answers (1)

benui
benui

Reputation: 6808

I've not seen that method of defining models using Mongoose before.

Have you tried using their model definition guide? Converting it to Coffeescript should be relatively easy.

Upvotes: 1

Related Questions