EJS comparison between two same strings - javascript

I try in ejs to compare two Strings which in the current situation are the same and it runs the else case. Here is the code:
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf == ids){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>
It logs 5d5ecfd1ad6d193de86c2264 - 5d5ecfd1ad6d193de86c2264

Use === instead of == to see if this fixes the issue, as it will prevent internal type coercion.
It might also be worth trimming the strings to see if there is any white space or padding added to them.

You could use the below code to compare two string values in ejs
<% if("some text" === "some text"){ %>
<h1>Yes the above text are equal</h1>
<% } %>

If you are comparing mongoDB objectId, then use toString().
<% var idf = notifications.data[i].from; %>
<% var ids = notifications.users[a].id; %>
<% if(idf.toString() == ids.toString()){ %>
<% index = a; %>
<script>alert('<%= index %>');</script>
<% break; %>
<% }else{ %>
<script>console.log('<%= idf %> - <%= ids %>');</script>
<% } %>

Related

How to place ejs in loops

People is an array of objects having firstName andlastName
<% for (var i = 0; i < people.length; i++) { %>
<%= firstName %> <%= lastName %>
<% } %>
Why end of %> is after loop { brakcet.
What is the need of ejs in end bracket of loop <% }%>

Rails js.erb executing within if statement that is false

animal.js.erb
<% if #animal.type == "Cat" %>
<% if #animal.meow == "Loud" %>
alert("Loud Cat");
<% else %>
alert("Quiet Cat");
<% end %>
<% else %>
<% if #animal.bark == "Ferocious" %>
alert("Ferocious Dog");
<% else %>
alert("Nice Dog");
<% end %>
<% end %>
When #animal.type == "Dog" I stil get an error for #animal.meow method does not exist.
Why is it reading that if statement and erroring?
It appears that .erb is executing the if #animal.meow statement even when the #animal.type is "Dog"
you forgot to add closing quote in line <% if #animal.meow == "Loud %> and in <% if #animal.bark == "Ferocious %>

How to add in else statement in underscore template

I have the following code:
var c = 'Credits: <% if (credits) { %> <%= credits %> <% } %> <% else { %> N/A <% } %>'
However I get Unexpected token else. Is the else statement different than how the if statement is added? What should the above correctly be?
Just get rid of the %> <% between } and else. Like this:
var c = 'Credits: <% if (credits) { %> <%= credits %> <% } else { %> N/A <% } %>';
Alternatively, the ternary operator is one of my faves:
var c = 'Credits: <%= credits ? credits : "N/A" %>';
In case it's unclear, the ternary is basically a reduced if/else statement. The part before the ? is the expression being evaluated for truthiness. If it's truthy, the middle part between ? and : is executed, but if it's falsey, the last part after : is executed instead.
A better way to do this would be with the OR operator:
var c = 'Credits:</b> <%= credits || "No credits" %></div>

Nested .each on embedded ruby not printing anything

I'm having some problems with embedded ruby. I'm trying to iterate through some variable, to print some JavaScript code.
This is the following code:
<% color1 = '#ff0000' %>
<% color2 = '#00ff00' %>
<% color3 = '#ffff00' %>
<% i = 0 %>
<% j = 0 %>
<% #chapters.each do |chapter| %>
sigInst.addNode('cap<%= chapter.reference %>',{
label: 'cap <%= chapter.reference %>',
color: '<%= j%2==0 ? color1 : color2 %>',
x: <%= i = i %>,
y: <%= i = i %>
}).draw();
<% i = i + 0.1 %>
<% j=j+1 %>
<% end %>
<% #chapters.each do |chapter| %>
<% chapter.decisions do |decision| %>
sigInst.addEdge('cap<%= chapter.reference %>_cap<%= decision.destiny_num %>','cap<%= chapter.reference %>','cap<%= decision.destiny_num %>').draw();
<% end %>
<% end %>
I'm using Sigma js to implement a graph. The first .each appears in the view, but the second part doesn't print anything. What's happening? Thanks
You forgot to do a .each on your decisions.
So the fixed line would be: <% chapter.decisions.each do |decision| %>
Ruby has the sometimes annoying feature of allowing you to pass a block to anything, whether it uses it or not. So in this case, you're just passing the block to the decisions method, which does nothing with it.

using if statement in each-loop

I have a loop
<% _.each(kw, function (x) { %>
now I want to use a if statement which checks a string
<% if ( <%=x%> == "condition") { %>
<div>..</div>
<% } %>
but doesn't work :( any ideas?
another question:
<script>
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
</script>
is declared above and what would be the correct if statement when I want to use maxKW
<% if ( <%=x%> == maxKW) { %>
<div>..</div>
<% } %>
would be great if you could help me :)
<% if (x === "condition") { %>
<div>..</div>
<% } %>
Pass maxKW as attribute to the compiled template:
var maxKW = $("[id^='kalenderwoche']:first").attr("id");
var html = _.template('your html')({maxKW:maxKW});

Categories