正在拜读https://github.com/nswbmw/N-blog,看到这里 出现了难以解决的错误:
首先我的本地数据库是开启了用户验证的,这个例子暂时是没有的,请看我的本地入口文件:
·
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var flash = require('connect-flash');
var config = require('config-lite');
·
·
·
app.use(session({
name: config.session.key,// 设置 cookie 中保存 session id 的字段名称
secret: config.session.secret,// 通过设置 secret 来计算 hash 值并放在 cookie 中,使产生的 signedCookie 防篡改
cookie: {
maxAge: config.session.maxAge// 过期时间,过期后 cookie 中的 session id 自动删除
},
store: new MongoStore({// 将 session 存储到 mongodb
url: config.mongodb// mongodb 地址
})
}));
// flash 中间价,用来显示通知
app.use(flash());
// 设置模板全局常量
app.locals.blog = {
title: pkg.name,
description: pkg.description
};
// 添加模板必需的三个变量
app.use(function (req, res, next) {
res.locals.user = req.session.user;
res.locals.success = req.flash('success').toString();
res.locals.error = req.flash('error').toString();
next();
});
·
·
然后是为config创建的默认文件:
module.exports = {
port: 3000,
session: {
secret: 'myblog',
key: 'myblog',
maxAge: 2592000000
},
mongodb: 'mongodb://localhost:27017/myblog'
};
可以看出我这里没有用户验证,所以运行程序后显示
Unhandled rejection MongoError: not authorized on myblog to execute command { listIndexes: "sessions", cursor: {} }
at Function.MongoError.create (E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:31:11)
at queryCallback (E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:213:36)
at E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:455:18
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
我在本地新建了myblog数据库,然后添加了用户
> use myblog
switched to db myblog
> show users
{
"_id" : "myblog.test",
"user" : "test",
"db" : "myblog",
"roles" : [
{
"role" : "dbOwner",
"db" : "myblog"
}
]
}
{
"_id" : "myblog.myblog",
"user" : "myblog",
"db" : "myblog",
"roles" : [
{
"role" : "dbAdmin",
"db" : "myblog"
}
]
}
并且更改了接入的mongodb的地址:
module.exports = {
port: 3000,
session: {
secret: 'myblog',
key: 'myblog',
maxAge: 2592000000
},
mongodb: 'mongodb://myblog:myblog@localhost:27017/myblog'
};
这次运行没有报错,但是当我尝试打开任何页面时,又出错了:
MongoError: not authorized on myblog to execute command { find: "sessions", filter: { _id: "m0DdUbQOkBt9if1E68C230avYqyCgpLU", $or: [ { expires: { $exists: false } }, { expires: { $gt: new Date(1478591799060) } } ] }, limit: 1, batchSize: 1, singleBatch: true }
at Function.MongoError.create (E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\error.js:31:11)
at queryCallback (E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:213:36)
at E:\learnNode\nodeBlog\node_modules\connect-mongo\node_modules\mongodb\node_modules\mongodb-core\lib\connection\pool.js:455:18
at nextTickCallbackWith0Args (node.js:420:9)
at process._tickCallback (node.js:349:13)
问了公司的人员,说是刚开始是链接的,但是可能又断开了,当我访问网页时就不能写入了?
所以请大家帮忙看看怎么回事?多谢了!
Just change it to the original username and password of our database today. The original user admin has root permissions, but the myblog user I created also has read and write permissions. I don’t know why it doesn’t work. I was struggling with this for a long time yesterday, but unexpectedly an idea suddenly came to me today.
First use the command line to log in and execute it to see if there will be any errors and locate the problem.
db.auth('myblog', 'myblog')
Whether it returns 0 or 1'mongodb://myblog:myblog@localhost:27017/myblog'
Is the space between user and pwd the extra space you copied? ? ? ? ? ? ? ? Or maybe you wrote the wrong connection string in your code@watch echo I also worked on this project and encountered the same problem. I didn’t understand the answer below. How to solve it?