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
^
Reference开发者_如何学GoError: host is not defined
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.
精彩评论