使用 MongoDB 在 Node.js 中定义“模型”的正确方法是什么?
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我以前没有见过使用 Mongoose 定义模型的方法。
您是否尝试过使用他们的模型定义指南?将其转换为 Coffeescript 应该相对容易。
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.