lundi 17 septembre 2018

Send data from routes to html in nodejs

I'm currently working on a nodejs project and I want to show some content in index.ejs depending if the user is logged in or not. I truly have google it but nothing works, I would really appreciate if you could tell me what's wrong. Right now it show me an Error of: isLoggedIn is not defined

Here's the html

const Location = require('../models/location');

module.exports = (app, passport) => {



app.get('/', isLoggedIn, (req, res) => {
    res.render('index', {
         user: req.user, isLoggedIn: req.isLogged
    });
});


app.get('/profile', isLoggedIn, (req, res) => {
    res.render('profile', {
        user: req.user
    });
});

app.get('/profile', isLoggedIn, (req, res) => {
    res.render('profile', {
      user: req.user, isLoggedIn: req.isLogged
    });
});


app.get('/getUsername',isLoggedIn,(req, res) => {
    res.json({username: req.user.email});
});

app.get('/getLocations', isLoggedIn, (req, res) => {
    Location.find({user:req.user.email},function(err, locations){
        res.json({locations:locations});
    });
});

app.post('/location', (req, res) => {
    let location = new Location()
    console.log(req.body)
    location.user = req.body.email
    console.log("LLegue al post")
    location.latitude = req.body.latitude
    location.longitude = req.body.longitude
    console.log(location.user)
    console.log(location.latitude)
    console.log(location.longitude)

    location.save((err, LocationStored)=>{
        if(err) res.status(500).send({message: `Error al salvar en la base de datos: ${err}`})
    })

    res.json({message: "Exitasion"});
});   
};

function isLoggedIn(req, res, next) {
   if (req.isAuthenticated()){
      return next();
   }
 return res.redirect('/');
}

and my index.ejs is:

<% if (isLoggedIn) { %>
//code
  <% } else{ %>  
<p>you're logged in</p>
 <% } %>




Aucun commentaire:

Enregistrer un commentaire