20
problem in flash message
my main app.js
require('dotenv').config();
const express = require('express');
const engine = require('ejs-mate');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const favicon = require('serve-favicon');
const bodyParser = require('body-parser');
const passport = require('passport');
const User = require('./models/user');
const session = require('express-session');
const mongoose = require('mongoose');
const methodOveride = require('method-override');
//configur passport and session
app.use(session({
secret: 'syukri',
resave: false,
saveUninitialized: true,
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
// set local variables middleware
app.use(function(req, res, next) {
// set default page title
res.locals.title = 'Sistem Siswa';
// set success flash message
res.locals.success = req.session.success || '';
delete req.session.success;
// set error flash message
res.locals.error = req.session.error || '';
delete req.session.error;
// continue on to next function in middleware chain
next();
});
the flash-message.ejs file
<% if(success && success.length) { %>
<%= success %>
<% if(error && error.length) { %>
<%= error %>
the show.ejs file
<% layout('layouts/post-show-layout') -%>
Price:
<img src="<%=%20image.url%20%>" alt="Surf board image">
<a href="/posts/<%=%20post.id%20%>/edit">
<button>Edit</button>
</a>
the show-post-layout.ejs file
<!DOCTYPE html>
the controller post.js file
// Posts Create
async postsCreate(req, res, next) {
req.body.post.images = [];
for(const file of req.files) {
let image = await cloudinary.uploader.upload(file.path);
req.body.post.images.push({
url: image.secure_url,
public_id: image.public_id
})
}
let response = await geocodingClient
.forwardGeocode({
query: req.body.post.location,
limit: 1
}). send();
req.body.post.coordinates = response.body.features[0].geometry.coordinates;
let post = await Post.create(req.body.post);
req.session.success = 'Post created successfully!';
res.redirect(/posts/${post.id}
);
},
20