Express.js:存储在`res.locals`中的变量未在视图中定义 [英] Express.js : variable stored in `res.locals` isn't defined in the view

查看:36
本文介绍了Express.js:存储在`res.locals`中的变量未在视图中定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在一个 express 应用程序中,我使用了一个在 app.js 中声明的 express 路由器:

In an express application, i'm using an express router declared in the app.js as:

var adminRouter = require('./admin');
app.use("/admin", adminRouter); 

所以它引用了 admin.js 文件,我在其中定义了这个特定的路由:

so it references the admin.js file in which i defined this specific route :

 router.route('/rooms/edit/:id')
    .all(function(req, res, next) {
        var roomid = req.params.id;
        var room = _.find(rooms, r => r.id == roomid);
        if (!room) {
            res.sendStatus(404);
            return;
        }
        res.locals.room = room;
        next();
    }).get(function(req, res) {

        res.render('edit', { room: room });
    }).post(function(req, res) {

        res.locals.room.name = req.body.name;
        res.redirect('./'); 
    });

但是在用于编辑房间名称的视图中(通过上述路由中的 .get 方法访问),我收到此错误,指出我存储在 res.locals<中的 room 变量/code> 未定义:

but in the view for editing the room name (accessed by the .get method in the route above) i get this error saying that the room variable i stored in res.locals isn't defined :

ReferenceError: room is not defined

这里是整个 app.js

var express = require('express');
var app = express();
var bodyParser =  require('body-parser');


app.set("views", "./views");
app.set("view engine", "jade");

app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));


app.use(function(req, res, next){
    console.log('incomming req : ' + req.url);
    next();
});

app.get('/', function(req, res){
    res.render('index', {title:'Home'});
});

var adminRouter = require('./admin');
app.use("/admin", adminRouter);

app.listen(3000, function(){
    console.log('app running on port 3000');
});

这也是 edit.jade 视图:

and this is the edit.jade view too :

extends layout
block content
 h1 Edit chatroom!!
 form(method="POST")
      fieldset.form-group
          label(for="name") Name:
          input.form-control(name="name", type="text", placeholder="Enter a name" value="#{room.name}")
          small.text-muted Give your chatroom a meaningful name for people to refer to it.
      button.btn.btn-primary(type="submit") Save chatroom
      a.btn.btn-default(href="/admin/rooms") Cancel     

推荐答案

我收到这个错误,说我存储在 res.locals 中的 room 变量没有定义:

i get this error saying that the room variable i stored in res.locals isn't defined :

ReferenceError: room is not defined

那不是错误所说的:它说一个名为 room变量没有定义.它还应该将您指向发生错误的代码行,这里是:

That's not what the error says: it says that a variable called room isn't defined. It should have also pointed you to the line of code where the error happens, which is here:

res.render('edit', { room: room });

事实上,room 未定义的地方.

Where, in fact, room is undefined.

这篇关于Express.js:存储在`res.locals`中的变量未在视图中定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆