一个不大的坑, 刚学会设置了--auth
之后, 就去找关于认证信息的部分
网上搜去可能用Db.authenticate
来设置登陆信息, 不过却有问题的
http://stackoverflow.com/questions/4688693/node-js-mongodb
host = 'localhost'
port = 27017
mongodb = require 'mongodb'
mongoserver = new mongodb.Server host, port, {}
db_connector = new mongodb.Db 'Share', mongoserver, {}
db_connector.authenticate 'nodejs', 'Passwd', ->
console.log 'done'
db_connector.open (err, db) ->
throw err if err?
db.collection 'test', (err, collection) ->
throw err if err?
collection.save test:'data', (err, doc) ->
console.log doc
有个报错:
[ERROR] TypeError
TypeError: Cannot call method 'getAllConnections' of undefined
这里什么问题呢?
反正先搜索解决方案, 找到这个…
https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/0_4K9RAlddY
https://github.com/gatesvp/cloudfoundry_node_mongodb/blob/master/app.js
这边用是换用另一种方案了, 我抄过来, 然后执行save
成功了
url = 'mongodb://nodejs:Passwd[@localhost](/user/localhost):27017/Share'
(require 'mongodb').connect url, (err, db) ->
db.collection 'test', (err, collection) ->
collection.save save: 'tests', ->
console.log 'done'
collection.find {}, (err, cursor) ->
cursor.each (err, docs) ->
console.log docs