使用MongoDB在Node.js中定义`model`的正确方法是什么?

时间:2011-09-14 03:05:07

标签: mongodb node.js express

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

我在一个名为locations.coffee的文件中,在app.js中,我有

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

但显然它永远不会被实例化,因为我得到了

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

1 个答案:

答案 0 :(得分:1)

我之前没有看到使用Mongoose定义模型的方法。

您是否尝试过使用model definition guide?将它转换为Coffeescript应该相对容易。