nodejs在学习使用jade模板、express框架及与mongodb数据库进行交互,做一个用户验证网站时遇到问题?
网站能够正常连接数据库,各项配置应该均正确,但是在注册用户时无法保存至数据库并出现了如下问题,不太明白哪里undefined了。
E:\code\user-auth-example\node_modules\mongodb\lib\utils.js:98
process.nextTick(function() { throw err; });
^
TypeError: Cannot read property 'email' of undefined
at E:\code\user-auth-example\server.js:38:34
at E:\code\user-auth-example\node_modules\mongodb\lib\collection.js:525:5
at E:\code\user-auth-example\node_modules\mongodb\lib\collection.js:703:5
at handleCallback (E:\code\user-auth-example\node_modules\mongodb\lib\utils.js:95:56)
at executeCommands (E:\code\user-auth-example\node_modules\mongodb\lib\bulk\ordered.js:398:12)
at resultHandler (E:\code\user-auth-example\node_modules\mongodb\lib\bulk\ordered.js:425:5)
at E:\code\user-auth-example\node_modules\mongodb-core\lib\connection\pool.js:455:18
at _combinedTickCallback (internal/process/next_tick.js:67:7)
at process._tickCallback (internal/process/next_tick.js:98:9)
然后下面是代码,比较多,package.json, express按照书上的方法用的是旧版本:
{
"name":"user-auth-example",
"version":"0.0.1",
"dependencies":{
"express":"2.5.8",
"mongodb":"2.2.24",
"jade":"0.31.2"
}
}
server.js:
var express=require('express'),Db = require('mongodb').Db,Server = require('mongodb').Server
app=express.createServer();
var db = new Db('test01', new Server('localhost', 27017));
db.open(function(err,client){
if(err)
throw err;
console.log('\033[96m + \033[39m connected to mongodb');
app.users=db.collection('users');
app.listen(3000,function(){
console.log('\033[96m + \033[39m app listening on *:3000');
});
});
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret:'my secret'}));
app.set('view engine','jade');
app.set('view options',{layout:false});
app.get('/',function(req,res){
res.render('index',{authenticated:false});
});
app.get('/login/:signupEmail?', function (req, res) {
res.render('login', { signupEmail: req.params.signupEmail });
});
app.get('/signup',function(req,res){
res.render('signup');
});
app.post('/signup', function (req, res, next) {
app.users.insert(req.body.user, {safe:true}, function(err,doc) {
if (err) return next(err);
res.redirect('/login/'+doc[0].email);
});
});
四个放在当前目录下views文件夹的jade,laydout.jade:
doctype 5
html
head
title MongoDB example
body
h1 My first MongoDB app
hr
block body
index.jade:
include layout
block body
if (authenticated)
p Welcome back, #{me.name}
a(href="/logout") Logout
else
p Welcome new visitor!
ul
li: a(href="/login") Login
li: a(href="/signup") Signup
login.jade:
include layout
block body
if (signupEmail)
p Congratulations on signingup! Please login below.
form(action="/login", method="POST")
fieldset
legend Log in
p
label Email
input(name="user[email]", type="text", value=signupEmail)
p
label Password
input(name="user[password]", type="password")
p
button Submit
p
a(href="/") Go back
signup.jade:
include layout
block body
form(action="/signup", method="POST")
fieldset
legend Sign up
p
label First
input(name="user[first]", type="text")
p
label Last
input(name="user[last]", type="text")
p
label Email
input(name="user[email]", type="text")
p
label Password
input(name="user[password]", type="password")
p
button Submit
p
a(href="/") Go back
最后吐槽一下Smashing Node.js这本书,真的太多错误了。然后若有大神能够回答此问题真的感激不敬!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
insert方法的RESULT不是你插入的doc,详细你可以查看官方api:链接描述
api建议使用insertOne不建议使用insert
上面你的注册代码,其中
.insert(req.body.user, {safe:true},...
insert
中插入出了问题,req.body.user
,键名呢你最好看下这个app.users.insert方法,返回的是什么
建议你如果想要一个比较全面的node教程可以去看这个nswbmw/N-blog一起学 Node.js