【问题标题】:Expressjs, Handlebars(hbs), express-messages, flash-connect. Not able to render flash messagesExpressjs、Handlebars(hbs)、express-messages、flash-connect。无法呈现 Flash 消息
【发布时间】:2017-06-02 10:12:33
【问题描述】:

我正在做一个快速应用程序,我想在用户使用表单注册时显示 Flash 消息。

我使用的技术是 Node.js、Express.js、Handlebars(hbs)、connect-flash 和 express-messages。

为了更容易找到错误,我以最简单的方式创建了一个新应用。

错误是: Error: /Applications/MAMP/htdocs/hbs-simple/views/index.hbs: Parse error on line 7: ...</head><body> {{{messages()}}} {{{b ---------------------^ Expecting 'ID', 'STRING', 'NUMBER', 'BOOLEAN', 'UNDEFINED', 'NULL', 'DATA', got 'INVALID'

项目结构:

├── app.js
├── package.json
└── _views
    ├── index.hbs
    ├── layout.hbs
    └── test.hbs

app.js:

const express = require('express');
const app = express();
const path = require('path');
const session = require('express-session');
const flash = require('connect-flash');
const hbs = require('hbs');

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');

app.use(require('connect-flash')());
app.use((req, res, next) => {
  res.locals.messages = require('express-messages')(req, res);
  next();
});

// Handle Sessions
app.use(session({
  secret: 'secret',
  saveUninitialized: true,
  resave: true
}));

app.get('/', (req, res, next) => {
  req.flash('success', 'Rendering index...');
  res.render('index');
});

app.get('/test', (req, res, next) => {
  req.flash('success', 'Rendering test...');
  res.render('test');
});

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

layout.hbs

<!DOCTYPE html>
<html>
<head>
  <title>{{title}}</title>
</head>
<body>
  {{{messages()}}}
  {{{body}}}
  <a href="/">index</a>
  <a href="/test">test</a>
</body>
</html>

index.hbs

<h3>This is the index</h3>

test.hbs

<h3>This is the index</h3>

package.json

{
  "name": "hbs-simple",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "Rafa",
  "license": "ISC",
  "dependencies": {
    "connect-flash": "^0.1.1",
    "express": "^4.15.3",
    "express-messages": "^1.0.1",
    "express-session": "^1.15.3",
    "hbs": "^4.0.1",
    "path": "^0.12.7"
  }
}

这是来自您可以在 [https://github.com/expressjs/express-messages][1]

上找到的 express-messages 文档
Rendering Messages

Call the messages() function as specified by your rendering engine:

EJS:

<%- messages() %>
Jade:

!= messages()

使用 hbs 的应该是 {{{messages}}} ,但这并不像我预期的那样工作。

针对这种情况的任何想法或任何解决方案。

非常感谢!

【问题讨论】:

    标签: javascript node.js express handlebars.js rendering


    【解决方案1】:

    你需要在你的 app.js 中添加一个全局变量

    app.use((req, res, next)=>{
     app.locals.success = req.flash('success')
     next();
    });
    

    然后在您的路线中添加消息

    app.get('/test', (req, res, next) => {
      req.flash('success', 'Rendering test...');
      res.render('test');
    });
    

    你终于.hbs

    {{#if success}}
    {{success}}
    {{/if}}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-18
      • 2018-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多