Как показать флеш / тост сообщение в шаблоне ejs в koajs? - PullRequest
0 голосов
/ 12 мая 2019

Я хочу показать сообщение flash / toast в представлении коа, используя ejs.

Как определить глобальную переменную? Я получаю success_msg не определено.

Я добавил partials / _messages.html в шаблон представления и одним нажатием кнопки хочу показать флэш-сообщение.

index.js

const Koa = require('koa');
const bodyParser = require('koa-body');
const logger = require('koa-morgan')
const mongoose = require('mongoose');
const path = require('path');
const render = require('koa-ejs');
const Router = require('koa-router'); 
const session = require('koa-generic-session');
const flash = require('koa-better-flash');

const router = new Router();
const app =  module.exports = new Koa();

app.keys = [ 'keys' ];
app.use(session());
app.use(flash());
app.use(router.routes());
app.use(votes.routes()).use(votes.allowedMethods());


router.post('/messages', (ctx, next) => {
  // you can also pass an array of messages:
  // ctx.flash('info', [ 'hi', 'hello', 'good morning' ]);
  console.log('here');
  ctx.flash('info', 'hello world');
  ctx.flash('success_msg', 'hello world');
  ctx.status = 200;
});

router.get('/messages', ctx => {
  // to get all messages by type simply call `ctx.flash()`
  ctx.body = ctx.flash();
  // outputs: [ 'hello world ']
});



app.use( ctx => {
  ctx.success_msg = ctx.flash('success_msg');
});

const PORT = process.env.PORT || 5000;

app.listen(PORT, console.log(`Server started on port ${PORT}`));

Часть маршрута

router.post('/votes/managervote', castManagerVote);
async function castManagerVote (ctx) {
  console.log(ctx.request.body);
  let vote = new Vote();
  vote.vote_category = 'Hiring manager';
  vote.incoming_vote = ctx.request.body.incoming_vote; 
  await vote.save()
          .then(data=> {
            ctx.flash('success_msg', 'ManagerDone');
            ctx.redirect('/votes');
          });
}
...