无法读取未定义的属性'toastr'

时间:2018-04-16 01:18:09

标签: node.js express undefined toastr

我一直在尝试将express-toastr实现为我的快速js表单并使用它来发送express-validator错误消息作为吐司通知,但是当我将所需的div #{req.toastr.render()}放入我的index.pug文件中时尝试加载页面时收到错误Cannot read property 'toastr' of undefined

完整错误:

        TypeError: /home/bob/project/views/index.pug:34
    32|         h1(class="section__title") Title1
    33|         div(class="centerBlock") 
> 34|           div #{req.toastr.render()}
    35|             .col-xs-12
    36|               form(action='', method='post', id='form')
    37|                 .row

Cannot read property 'toastr' of undefined
    at eval (eval at wrap (/home/bob/project/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:117:61)
    at template (eval at wrap (/home/bob/project/node_modules/pug-runtime/wrap.js:6:10), <anonymous>:245:131)
    at Object.exports.renderFile (/home/bob/project/node_modules/pug/lib/index.js:428:38)
    at Object.exports.renderFile (/home/bob/project/node_modules/pug/lib/index.js:418:21)
    at View.exports.__express [as engine] (/home/bob/project/node_modules/pug/lib/index.js:465:11)
    at View.render (/home/bob/project/node_modules/express/lib/view.js:135:8)
    at tryRender (/home/bob/project/node_modules/express/lib/application.js:640:10)
    at Function.render (/home/bob/project/node_modules/express/lib/application.js:592:3)
    at ServerResponse.render (/home/bob/project/node_modules/express/lib/response.js:1008:7)
    at /home/bob/project/routes/index.js:19:7
    at Layer.handle [as handle_request] (/home/bob/project/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/bob/project/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/bob/project/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/bob/project/node_modules/express/lib/router/layer.js:95:5)
    at /home/bob/project/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/bob/project/node_modules/express/lib/router/index.js:335:12)

我的layout.pug如下:

link(rel="stylesheet" href="/css/toastr.css")
script(src="/js/jquery.js")
script(src="/js/toastr.js")

和我的app.js:

var cookieParser = require('cookie-parser');
var session = require('express-session');
var flash = require('connect-flash');
var toastr = require('express-toastr');

var index = require('./routes/index');

var app = express();
app.use(cookieParser('secret'));
app.use(session({
    secret: 'secret',
    saveUninitialized: true,
    resave: true
}));
app.use(flash());
app.use(toastr());

app.use('/js', express.static(__dirname + 
'/node_modules/jquery/dist')); // redirect JS jQuery + toastr.js is in this folder
app.use('/css', express.static(__dirname + 
'/node_modules/bootstrap/dist/css')); // redirect CSS bootstrap;

index.js:

router.get('/', function(req, res, next) {
res.render('index', { title: 'Project' });
});

router.post('/', function(req, res){

var errors = req.getValidationResult();

if (errors){
    for (error in errors){
      req.toastr.error(error.msg)
    }
    res.redirect(req.get('referer'));
    return;
  }
})

我对nodejs比较新,但到目前为止我一直在管理,但是我已经尝试过并且无法弄清楚为什么这不起作用。如果我遗漏了一些非常简单的东西,请道歉。

干杯, 凯尔。

1 个答案:

答案 0 :(得分:0)

问题是您没有传递 req 对象来查看渲染功能。 将 index.js 获取方法更改为以下内容:

router.get('/', function(req, res, next) {
    res.render('index', { 
        title: 'Project',
        req: req });
});

可以找到更多使用文档here