无法使用猫鼬获取集合名称

unable to get collection names with mongoose

此 Ruby 代码打印 "db" 中的集合名称。尝试对咖啡做同样的事情

require 'mongo'
include Mongo
mongocl = MongoClient.new("localhost")
p mongocl["db"].collection_names
mongocl.close

等效的 coffeescript 代码(不确定)。

mongoose = require 'mongoose'
mongoose.connect 'localhost/db'
mongoose.connection.on 'open', (err) ->
  mongoose.connection.db.collectionNames (err, data) ->
    console.log data
    return 
mongoose.connection.close()

有人能指出其中的错误吗? 我正在使用最新的 mongoose@3.8.x

问题是您在 open 事件处理程序有机会执行之前关闭连接。

close() 调用移到传递给 collectionNames 的回调中:

mongoose = require 'mongoose'
mongoose.connect 'localhost/db'
mongoose.connection.on 'open', (err) ->
  mongoose.connection.db.collectionNames (err, data) ->
    console.log data
    mongoose.connection.close()