在节点中重用 MongoDB 连接
Reusing MongoDB connection in node
这是我得到的错误:[Error: db object already connecting, open cannot be called multiple times]
。我在这个誓言测试中有一个全局 mongo 对象。
mongo = new mongo.Db(config.api.apiTest, new mongo.Server('localhost', 27017, {}), {native_parser: off, safe: true})
当我第二次尝试打开它时出现此错误。
所以即使我第一次使用 db.close()
它似乎也没有关闭。是否有另一种重新使用连接的方法?
.addBatch(
"Redis - ":
"Standard account - ":
topic: ->
# .
redisClient.del "#{ accountId }:900"
mongo.open (error, db) =>
secondDb = db.db config.api.accountTest
secondDb.collection 'accounts', (error, accountsColl) =>
accountsColl.update {'accountId': ObjectID(accountId)}, { "$unset": {'permission': ''}}, (error, records) =>
console.log "Unset? " + records
db.close()
db.close()
inventory = nock('http://inventory.honshuu.vgrnt:80')
.get("/devices/?accountId=#{ accountId }&userId=#{ userId }")
.reply(200, 'OK')
httpRequest.get 'http://127.0.0.1:18091/inventory/devices/?token=testtoken', @callback
return
"Ratelimit is applied correctly":
(error, response, body) ->
accountLimit = response.headers['x-ratelimit-limit']
assert.equal response.headers['x-ratelimit-remaining'], 240-1
assert.equal response.headers['x-ratelimit-reset'], 900
"account":
topic: ->
# Need to fetch permission in db.
# redisClient.del "permission-#{ accountId }", (error, result) =>
# console.log "removes"
# console.log result
inventory = nock('http://inventory.honshuu.vgrnt:80')
.get("/devices/?accountId=#{ accountId }&userId=#{ userId }")
.reply(200, 'OK')
mongo.open (error, db) =>
secondDb = db.db config.api.accountTest
console.log secondDb
secondDb.collection 'accounts', (error, accountsColl) =>
accountsColl.update {'accountId': ObjectID(accountId)}, { 'permission': 1000}, (error, records) =>
console.log "updated? " + records
httpRequest.get 'http://127.0.0.1:18091/inventory/devices/?token=testtoken', @callback
return
return
"account has more rate tokens":
(error, response, body) ->
console.log response
console.log error
# accountLimit = response.headers['x-ratelimit-limit']
# assert.equal response.headers['x-ratelimit-remaining'], 1000-1
# assert.equal response.headers['x-ratelimit-reset'], 900
# assert.equal response.headers['x-ratelimit-limit'], 1000
)
就像节点中的大多数东西一样,db.close
是异步的并接受回调。您需要传递回调或对其进行承诺并 return 承诺。您的下一个连接调用只能在您确定数据库连接已关闭时完成,并且您只能通过处理回调知道这一点。
db.close(function (err) {
if (err) return console.error('OH NOES, AN ERROR:', err);
console.log('SUCCESS!');
// db is now closed, do whatevs
});
这是我得到的错误:[Error: db object already connecting, open cannot be called multiple times]
。我在这个誓言测试中有一个全局 mongo 对象。
mongo = new mongo.Db(config.api.apiTest, new mongo.Server('localhost', 27017, {}), {native_parser: off, safe: true})
当我第二次尝试打开它时出现此错误。
所以即使我第一次使用 db.close()
它似乎也没有关闭。是否有另一种重新使用连接的方法?
.addBatch(
"Redis - ":
"Standard account - ":
topic: ->
# .
redisClient.del "#{ accountId }:900"
mongo.open (error, db) =>
secondDb = db.db config.api.accountTest
secondDb.collection 'accounts', (error, accountsColl) =>
accountsColl.update {'accountId': ObjectID(accountId)}, { "$unset": {'permission': ''}}, (error, records) =>
console.log "Unset? " + records
db.close()
db.close()
inventory = nock('http://inventory.honshuu.vgrnt:80')
.get("/devices/?accountId=#{ accountId }&userId=#{ userId }")
.reply(200, 'OK')
httpRequest.get 'http://127.0.0.1:18091/inventory/devices/?token=testtoken', @callback
return
"Ratelimit is applied correctly":
(error, response, body) ->
accountLimit = response.headers['x-ratelimit-limit']
assert.equal response.headers['x-ratelimit-remaining'], 240-1
assert.equal response.headers['x-ratelimit-reset'], 900
"account":
topic: ->
# Need to fetch permission in db.
# redisClient.del "permission-#{ accountId }", (error, result) =>
# console.log "removes"
# console.log result
inventory = nock('http://inventory.honshuu.vgrnt:80')
.get("/devices/?accountId=#{ accountId }&userId=#{ userId }")
.reply(200, 'OK')
mongo.open (error, db) =>
secondDb = db.db config.api.accountTest
console.log secondDb
secondDb.collection 'accounts', (error, accountsColl) =>
accountsColl.update {'accountId': ObjectID(accountId)}, { 'permission': 1000}, (error, records) =>
console.log "updated? " + records
httpRequest.get 'http://127.0.0.1:18091/inventory/devices/?token=testtoken', @callback
return
return
"account has more rate tokens":
(error, response, body) ->
console.log response
console.log error
# accountLimit = response.headers['x-ratelimit-limit']
# assert.equal response.headers['x-ratelimit-remaining'], 1000-1
# assert.equal response.headers['x-ratelimit-reset'], 900
# assert.equal response.headers['x-ratelimit-limit'], 1000
)
就像节点中的大多数东西一样,db.close
是异步的并接受回调。您需要传递回调或对其进行承诺并 return 承诺。您的下一个连接调用只能在您确定数据库连接已关闭时完成,并且您只能通过处理回调知道这一点。
db.close(function (err) {
if (err) return console.error('OH NOES, AN ERROR:', err);
console.log('SUCCESS!');
// db is now closed, do whatevs
});