这让我疯了,请帮助我: 我正在使用带有express,mongo和EJS的最新版本的node.js 有趣的是,在控制台中出现以下错误后,网站仍可正常工作:
Cannot read property 'parent_category_id' of undefined
at eval (eval at <anonymous> (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:485:12), <anonymous>:20:27)
at returnedFn (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:514:17)
at View.exports.renderFile [as engine] (E:\BITBUCKET repos\demo nodeJS\node_modules\ejs\lib\ejs.js:358:31)
at View.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\view.js:126:8)
at tryRender (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\application.js:639:10)
at EventEmitter.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\application.js:591:3)
at ServerResponse.render (E:\BITBUCKET repos\demo nodeJS\node_modules\express\lib\response.js:961:7)
at E:\BITBUCKET repos\demo AndreiRadulescu nodeJS\Router.js:108:12
at handleCallback (E:\BITBUCKET repos\demo nodeJS\node_modules\mongodb\lib\utils.js:96:12)
at E:\BITBUCKET repos\demo
的NodeJS \ node_modules \ mongodb的\ lib中\ cursor.js:851:16
我的路由器:
router.get(
'/:categ/:subCateg' ,
function ( req , res )
{
var subC = req.params.subCateg;
var categ = req.params.categ;
req.app.locals.db.collection( "categories" )
.find
(
{ categories : { $elemMatch : { id : subC } } }
)
//gets only the categories we're interested in
.map( function ( u ) { return u.categories[ 0 ] } )
.toArray
(
function ( err , docs )
{
var toSend = docs[ 0 ];
//res.json( toSend );
res.render(
"subCategs" , {
_ : _ ,
docs : toSend ,
...
res.json(toSend)的结果是我所期望的:
正如我之前提到的,尽管有错误,但一切都在运行! 当获得/ api /具有相同错误时,它会中断。
我怀疑我的其他路线可能存在问题:
router.get(
'/:root/:topC/:prods' , function ( req , res )...
router.get(
'/:root/:topC/:subC/:prodId' ,function ( req , res )...
router.get(
'/api/:SOAPmethod' , function ( req , res ) ...
GET /
GET /mens/mens-clothing
GET /mens/mens-clothing/mens-clothing-suits
GET /mens/mens-clothing/mens-clothing-suits/25604524
GET /api/getall
TypeError: E:\BITBUCKET repos\demo nodeJS\views\subCategs.ejs:8
6| <title><%=title%></title>
7| <style>
>> 8| #<%=docs["parent_category_id"]%>
9| {
10| background: rgba(178, 190, 255, 0.9)
11| ;
Cannot read property 'parent_category_id' of undefined............
答案 0 :(得分:1)
问题是docs
未定义。是什么让你认为它被定义了?