【发布时间】:2014-09-03 23:08:19
【问题描述】:
我正在创建一个终端应用程序,它使用 mongoose 进行每次执行的单次插入和读取操作。
我使用以下代码作为我的应用程序的数据库工具
require '../objs/'
mongoose = require 'mongoose'
class DbManager
constructor: (@dbname="daftar") ->
start_connection: ()-> mongoose.connect "mongodb://localhost/#{@dbname}"
close_connection: ()-> mongoose.disconnect
getDatabaseName : ()-> return dbname
getDatabaseConnection : ()-> return mongoose.connection
saveNote : (noteObj)->
notes_model = mongoose.model "notes", DafNote.schema
note = new notes_model noteObj
note.save (err,dt)->
if err
console.log err
# register to node root
root.DbManager = DbManager
这是调用 db util 的部分
require './src/core/objs/'
require './src/core/data/'
noteObj = new DafNote "test_title","this is a test body"
dbman = new DbManager
dbman.start_connection
dbman.saveNote noteObj
dbman.close_connection
当我运行应用程序时,它会将对象保存到数据库中,但应用程序会一直在终端中等待并且不会关闭。
我已经追踪到问题,我发现mongoose.connect 函数是导致我的应用程序无法关闭的原因,即使我调用了mongoose.disconnect。
我真的认为可能是 connect 和 disconnect 函数的性质异步导致了这个问题。
如何解决这个问题,让应用程序在保存到 mongodb 后关闭,而不是在终端冻结?
【问题讨论】:
标签: node.js mongodb coffeescript mongoose