Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8686185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:46:54+00:00 2026-06-12T22:46:54+00:00

I initially created a show route for a basic function to get a specific

  • 0

I initially created a show route for a basic function to get a specific map from a mongodb database (using mongoose) and then rendering the map to the client-side with an .ejs template.

I then wanted to create functionality to favourite a map and add to a user’s favourites (with a user having its own model). As part of this, in the below show route I wanted to ensure that if a user already had a map in their favourites, different content would be rendered on the client-side to reflect that a map had already been favourite rather than continuing to display the option to favourite a particular route.

The method I have been trying to use is shown in the below code. This determines whether an authed user who has favourites is present (using passport-local and its req.user functionality) and then is designed to iterate over the favourites and render different variables if a user already has a map in their favourites.

I am coming up with problems mainly because the if/else routes will be called for all items in the array resulting in a conflict in terms of multiple res.render calls, which is obviously not my intention (it works ok where there is just one item in the array for example). However, I have attempted to amend the code to provide no else route but this still leaves the problem of how to deal with fall-through for rendering separate content where there is a authed user but whose favourites are not present.

I have also attempted to iterate over the favourites in the ejs template itself with the use of a partial through ejs-locals but this again doesn’t seem to work (I suspect for similar reasons as above).

So what I would like to know is whether the array iteration method is appropriate or viable and also a broader question of whether there is a better method for displaying different content on the same route dependent on certain server-side variables.

Client side code

<div class="row">

<% if (favourite == "No") { %>
    <div class="twelvecol" id="result"><div class="textbox" id="favourite">Favourite this route</div></div>

<% } else { %>
    <div class="twelvecol" id="result"><div class="textbox">This route is in your favourites</div></div>

Server side code

// Locates map on basis of ID in url before returning map, stringifying and sending to server

exports.show = function(req, res) {
var mapid = req.query.id;
console.log(mapid);
Map.findOne({_id: req.query.id}, function(err, map) {

         var jmap = JSON.stringify(map);
              if (req.user && req.user.favourites.length) {
                  favarray = req.user.favourites;
                  console.log(favarray);
                  for (var i = 0; i < favarray.length; i++) {
                        var favs = favarray[i];
                        if (favs == mapid) {
                           console.log('Favourite');
                           res.render('show', {obj: jmap, user: req.user, favourite: "Yes"})
                        }
                       else (favs !== mapid) {
                            console.log("Authed user but non-favourite")
                            res.render('show', {obj: jmap, user: req.user, favourite: "No"})
                         }
                      }
                   }
            else {
                    console.log("Either non-authed user or no existing favourites")
                    res.render('show', {obj: jmap, user: req.user, favourite: "No"});
                  }
    })
 };
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T22:46:55+00:00Added an answer on June 12, 2026 at 10:46 pm

    In a classical server side MVC sense it is absolutely okay to let the action or even view processing template engine adjust the dynamic portions of your response.

    As long as your user object is not exposed to the view (as an ejs local), you should get what you intended that way you started – with some important modifications: 1. Don’t render within the loop. (Why?! This must actually result in multiple rendering calls!) 2. Work with variables visible to the action block. 3. Skip the conditional following your else statement! (That works?! I’m curious about how this is evaluated by the parser. Normally you would do that with an else if (condition) or use a switch – but you don’t need that in your a/b case!).

    A proposal for a step to a cleaner code (only the req.user case):

    var fav = false;
    
    for (var i = 0; i < favarray.length; i++) {
      if (favarray[ i ] == mapid) {
        fav = true;
        break;
      }
    }
    
    res.render('show', {obj: jmap, user: req.user, favourite: fav})
    

    As an additional advice: Use booleans instead of strings for boolean variables like favourite and handle them as shown below in your ejs template:

    <% if (favourite) { %>
      ...
    <% } else { %>
      ...
    

    Also you should probably adjust your first if-condition checking if user exists and if the user has favorite items: If you want to handle these two conditions separately, you should not combine them in one condition statement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As I've come to understand using $('.whatever').click() only works for items created initially. Additional
I am using MVC4's WEB API to expose a controller. Initially I created created
We have few components like libraries dlls When initially created I ran the following
What objects are created initially by compilers(?) of javascript? I've been learning Io in
If a VS2008 project is created initially with a web app project, and class
I need to decrypt a conection that was created initially under an account that
I've just created a new Virtual Machine, which was initially completely clean - I've
I'm working with an NSImage which comes from a PDF. When I initially create
I want to create an iPad app which will initially show a Table View
Before you react from the gut, as I did initially, read the whole question

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.