how do i solve 'Cannot read properties of undefined' in ejs - ejs

i am using ejs as the template language in 11ty. in trying to use 11ty collections, i came up with the following ejs code.
---
layout: layouts/base.ejs
title: a list of post
---
<ul>
<% const posts = collections.post;
for (let a_post in posts) { %>
<li> <%- a_post.data.title %> </li>
<% } %>
</ul>
this gives the error:
Cannot read properties of undefined (reading 'title')
the nunjucks code works however,
ul>
{%- for post in collections.post -%}
<li>{{ post.data.title }}</li>
{%- endfor -%}
</ul>
how can i write the above in ejs
i really have no idea of what i am doing wrong

The collections.post is what i would refer to as an objects of objects. The for/in loop iterates over properties of an obeject. there are two ways to address this
Use a for/of loop: this iterates over an iterable object
<ul>
<% const posts = collections.post;
for (let a_post of posts) { %>
<li> <%- a_post.data.title %> </li>
<% } %>
</ul>
Change code to the following
<ul>
<% const posts = collections.post;
for (let a_post in posts) { %>
<li> <%- (posts)[a_post].data.title %> </li>
<% } %>
</ul>

Related

display a message when mongoDB collection is empty

I have a notices section on my website which are stored in my Mongo Database. If the collection is empty, how would i display a message to say something like "No new notices"
I am looping this on my page using EJS my code is below.
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
I guess you can put a condition like so:
<% if (!notices.length) {%>
<p>desired text if no notice exists</p>
<% } else { %>
<% for (let notice of notices) {%>
<p class="lead"><%= notice.noticeText %>
<% } %>
<% } %}

Is it possible to call another parital to the harpe js parameter?

I'm harp js user and im curious if its possible to call another parital to the harp js parameter?
I would like to proceed with modularization to reuse the code.
It is difficult to use the parital function of harp to insert data into a module of a particular module into another module.
This is my sources:
//component/button.ejs
<button><%= lable %></button>
//component/tab.ejs
<div class="tab">
<div class="tab__nav">
<% for(var i in locals.payload){ %>
<div class="tab__nav-item"><%- locals.payload[i].name %></div>
<% }; %>
</div>
<div class="tab__content">
<% for(var i in locals.payload){ %>
<div class="tab__content-item">
<div class="container">
<%- locals.payload[i].content %>
</div>
</div>
<% }; %>
</div>
</div>
//page.ejs
<%- partial("component/tab",{
payload: [
{
name:'tab1',
content:`
<div><%= partial("component/button.ejs",{label:"btn"})%></div>
`
},
{
name:'tab2',
content: `
<div>111111</div>
`
}
]}) %>

Random posts in Hexo

I wanted to show 5 random posts on my home page using Hexo, but can't seem to get it working?! I changed 'date' to 'random' but didn't work.
The code:
<ul>
<% site.posts.sort('date', -1).limit(5).each(function(post){ %>
<li>
<%= post.title || '(no title)' %>
</li>
<% }) %>
</ul>
shuffle or the alias random will work:
<ul>
<% site.posts.random().limit(5).each(function(post){ %>
<li>
<%= post.title || '(no title)' %>
</li>
<% }) %>
</ul>
How it works:
Hexo uses Warehouse for its database. posts is a Query object. So to modify the posts in the future just find the right database Query method in the warehouse API. Each Query method returns a modified copy of the previous Query so that the methods can be chained. So if you want to modify it again just find another method and chain it. Hope this helps!

How do I use user_helper method globally throughout the app views?

I finished chapter 8 on Michael Hartl's Rails Tutorial, so I have the app set up to sign users up & sign in and out.
What I am trying to do user a User Helper method inside the application.html.erb (or more specifically inside a _header.html.erb partial).
I want to use the gravatar_for method (Defined in the user_helper.rb) to show the user's gravatar picture and users name instead of having the word "Account" with the drop down.
module UsersHelper
# Returns the Gravatar for the given user.
def gravatar_for(user, options = { size: 150})
gravatar_id = Digest::MD5::hexdigest(user.email.downcase)
size = options[:size]
gravatar_url = "http://gravatar.com/avatar/#{gravatar_id}?s=#{size}"
image_tag(gravatar_url, alt: user.name, class: "gravatar")
end
end
On the show.html.erb page I have the following code
<% provide(:title, #user.name ) %>
<div class="row">
<aside class="span6">
<section>
<h1>
<%= gravatar_for #user %>
<%= #user.name %>
</h1>
</section>
</aside>
</div>
And that shows the user's gravatar and name on their profile page. So what I tried to do was add those same methods to the header partial like so;
<% if signed_in? %>
<li id="fat-menu" class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">
<span id="small">
<%= gravatar_for #user %><%= #user.name %><b class="caret"></b>
</span>
</a>
<ul class="dropdown-menu">
<li><%= link_to "Profile", current_user %></li>
<li><%= link_to "Settings", '#' %></li>
<li class="divider"></li>
<li><%= link_to "Sign out", signout_path, method: "delete" %></li>
</ul>
</li>
<% else %>
<li id="cta"><%= link_to "+ Sign up", signup_path %></li>
<li><%= link_to "Sign in", signin_path %></li>
<% end %>
And that code shows up completely fine on the show.html.erb page, which I am assuming is because its the show action on the Users controller. But when I try to click to the home page or really any other page outside of the Users View then I get the error
NoMethodError in Pages#about
undefined method `email' for nil:NilClass
So I guess my question is how do I make the gravatar for method and the #user.name available to all the views throughout the application. I feel as if the answer should be fairly simple, but i'm pretty new to rails, so any help would be greatly appreciated!
Put the method in the ApplicationHelper instead of the UserHelper. Here is an example of something similar I did in my own app: https://github.com/NatashaTheRobot/tinysale/blob/master/app/helpers/application_helper.rb

Rhomobile search question

I try to make rhomobile search in Fixed DB.
But it returns ALL entries in DB instead of the ones with given QID
<%=#questions.id%>
<% #antworts = Antwort.find(:conditions=>{'qid'=>#questions.id})%>
<% #antworts.each do |antwort| %>
<li>
<a href="sdfsdf">
<%= antwort.antwort %>
</a>
</li>
<% end %>
</ul>
Any idea why?
You need to pass :first or :all in the first argument:
Antwort.find(:all, :conditions=>{'qid'=>#questions.id})
Or change the method to find_all:
Antwort.find_all(:conditions=>{'qid'=>#questions.id})