我試圖存儲用戶對我自己mongo
數據庫不是默認(auth0服務器)。Auth0自定義數據庫MongoDB的
下面是腳本:
function create (user, callback) {
mongo('mongodb://admin:[email protected]:27017/mydb', function (db) {
var users = db.collection('subscribers');
users.findOne({ email: user.email },
function (err, withSameMail) {
if (err) return callback(err);
if (withSameMail) return callback(new Error('the user already exists'));
user.password = bcrypt.hashSync(user.password, 10);
users.insert(user, function (err, inserted) {
if (err) return callback(err);
callback(null);
});
});
});
}
這是我得到的錯誤,當我嘗試創建一個用戶:
[Error] Error: socket hang up
at createHangUpError (_http_client.js:200:15)
at Socket.socketOnEnd (_http_client.js:285:23)
at emitNone (events.js:72:20)
at Socket.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:905:12)
at nextTickCallbackWith2Args (node.js:437:9)
at process._tickDomainCallback (node.js:392:17)
這是哪個驅動程序?什麼是'mongo',只是'require('mongodb')'的結果?如果節點本地驅動程序,則需要在['MongoClient'](http://mongodb.github.io/node-mongodb-native/2.1/api/MongoClient.html)上調用'.connect()'。你也在這裏寫一些非常低效的代碼。閱讀[「upserts」](https://docs.mongodb.org/manual/reference/method/db.collection.update/#upsert-option),它基本上在一個數據庫中調用你正在做的兩個(查找/插入不存在)。一些通用教程也可能是值得的。 –
當然,還有一個主要概念,即應用程序應該連接到數據庫「一次」,而不是每個單獨的函數調用。數據庫連接非常昂貴,因此應該在應用程序的生命週期中維護數據庫連接,而不是一直連接和斷開連接。 –