我正在创建一个待办事项列表应用,当我运行app.js
时,快递给我这个错误。
快速
500 TypeError: Object #<Object> has no method 'uid'
at Object.exports.current_user [as handle] (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\routes\index.js:95:34)
at next (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.methodOverride [as handle] (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:48:5)
at next (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.urlencoded [as handle] (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\middleware\urlencoded.js:41:37)
at next (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at Object.json [as handle] (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\middleware\json.js:42:37)
at next (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\proto.js:193:15)
at multipart (C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\middleware\multipart.js:97:37)
at C:\Users\steph_000\SkyDrive\Documents\Programs\Todo List App\todo\node_modules\express\node_modules\connect\lib\middleware\bodyParser.js:64:9
此外,脚本是
var utils = require( 'util' );
var mongoose = require( 'mongoose' );
var Todo = mongoose.model( 'Todo' );
exports.index = function ( req, res, next ){
var user_id = req.cookies ?
req.cookies.user_id : undefined;
Todo.
find({ user_id : user_id }).
sort( '-updated_at' ).
exec( function ( err, todos ){
if( err ) return next( err );
res.render( 'index', {
title : 'Express Todo Example',
todos : todos
});
});
};
exports.create = function ( req, res, next ){
new Todo({
user_id : req.cookies.user_id,
content : req.body.content,
updated_at : Date.now()
}).save( function ( err, todo, count ){
if( err ) return next( err );
res.redirect( '/' );
});
};
exports.destroy = function ( req, res, next ){
Todo.findById( req.params.id, function ( err, todo ){
var user_id = req.cookies ?
req.cookies.user_id : undefined;
if( todo.user_id !== req.cookies.user_id ){
return utils.forbidden( res );
}
todo.remove( function ( err, todo ){
if( err ) return next( err );
res.redirect( '/' );
});
});
};
exports.edit = function( req, res, next ){
var user_id = req.cookies ?
req.cookies.user_id : undefined;
Todo.
find({ user_id : user_id }).
sort( '-updated_at' ).
exec( function ( err, todos ){
if( err ) return next( err );
res.render( 'edit', {
title : 'Express Todo Example',
todos : todos,
current : req.params.id
});
});
};
exports.update = function( req, res, next ){
Todo.findById( req.params.id, function ( err, todo ){
var user_id = req.cookies ?
req.cookies.user_id : undefined;
if( todo.user_id !== user_id ){
return utils.forbidden( res );
}
todo.content = req.body.content;
todo.updated_at = Date.now();
todo.save( function ( err, todo, count ){
if( err ) return next( err );
res.redirect( '/' );
});
});
};
// ** express turns the cookie key to lowercase **
exports.current_user = function ( req, res, next ){
var user_id = req.cookies ?
req.cookies.user_id : undefined;
if( !user_id ){
res.cookie( 'user_id', utils.uid( 32 ));
}
next();
};
帮助!我无法解决这个问题,因为我已经在互联网上搜索过了!
答案 0 :(得分:0)
基本上错误发生在第一行。 95给定文件,即
res.cookie( 'user_id', utils.uid( 32 ));
它表示您通过util
导入的模块require( 'util' )
不包含uid
方法
您还可以查看util
here
util
模块没有uid
方法。
如果您想生成uid
,可以使用node-uuid
之类的库
https://github.com/broofa/node-uuid