开发者

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

开发者 https://www.devze.com 2023-04-04 12:41 出处:网络
mongoose = require \'mongoose\' class Locations constructor: @(host, port) -> @db = new mongoose \'locations\', new Server(host, port, {auto_reconnect: true}, {})
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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号