Working with node and ejs. Why might <%= no be recognised inside quotes "", no colour change - node.js

The issue is on the 3rd row below. With in the href""'s, there is text and an a js script syntax, <%=. for some reason, the <%= is not being recognised as a script insert.
Any idea's on why this is happening and how to fix? I'm using node.js, Express, MongoDB, Mongoose
<% if(users.length > 0) { %>
<% users.forEach(function(user){ %>
<a href="/getUsers/<%= user._id %>">
<tr>
<td><%= user.userId %></td>
<td><%= user.userName %></td>
<td><%= user.userOperator %></td>
<td><%= user.userEmail %></td>
<td><%= user.userMobile %></td>
<td><%= user.userAdminYN %></td>
</tr>
</a>
<% }) %>
<% }else{ %>
I tried adding the script syntax in, and was expecting to see it change colour as it does elsewhere in the core.

Use a template string:
<a href="<%= `/getUsers/${user._id}` %>">

Related

ejs variable increment not working

After initialize ejs variable, i cant change value and increment operator not working.
Here is my code:
<% results1.forEach(function(result1, index){ %>
<% results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
var i=1; %>
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>
In the above code I declared a variable i and bottom I added i++.
i should be declared outside forEach statement, otherwise is redeclared at every cicle
correct the code as above
<% results1.forEach(function(result1, index){ %>
<% var i=1; results2.forEach(function(result2, index1){ %>
<% if(result1.id==result2.parent)
{
<tr>
<td><%= index+1 %>.<%= i %></td>
<td><%= result2.title %></td>
<td><%= result1.title %></td>
<td align="center"><%= result2.views %></td>
</tr>
<% i++;
} %>
<% }); %>
<% }); %>

How can i fetch the buffer image from mongodb on ejb page

Storing data like follow in monogo db like as follow below
_id:5a43366cf6e0443e3c782181
coin_code:abc
wallet_balance:000000
wallet_name:xyz
image:Object
contentType:image/png
data:Binary('/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEhUSEhMV')
__v:0
fetching inage on ejs page
<% var i=1 %>
<% result.forEach(function(result){ %>
<tr class="odd gradeX">
<td><%= i++ %></td>
<td><span class="chat-img pull-left"><img src="<%= result.image.data %>" alt="Coin Image" class="img-circle"></span></td>
<td><%= result.wallet_name %></td>
<td><%= result.wallet_balance %></td>
<td><%= result.coin_code %></td>
</tr>
but according to above code i am not getting image i am getting output like this-
+�� )�p�&�
JH�Ʒ?�7ӏ)-����v�Z֥����J#�k�f��u�L��Ԅ�|�+g�g��y{4��uŏ��a���ȍȪ�FG#�)^��
R������������p��5�Xide&y&Y�I$#��#f�g�!�F����a���Ƹ_Y̱3i6�2�_¥�(�1�x�mӿ��'�Iޔ~(�����܎̃��΃d��f�Q
I store images as strings in Base64 format.
Example:
"image" : "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAN0..."
Then I show images on EJS pages like the below:
Example:
<img id="image" src=<%- image %>>
Hope this helps...

Express.js - get selected item rows from a table in ejs

I'm trying to get selected rows from a table in express js.
Here is a html+ejs table code I have in my ejs view file. I'm trying to get only the rows where the checkbox is checked.
<table class="table">
<thread class = "thread-inverse">
<tr>
<th>Item Id</th>
<th>Name</th>
<th>Category</th>
<th>Manufacturer</th>
<th>Supplier</th>
<th>Buying Price</th>
<th>Selling Price</th>
<th>Action</th>
</tr>
<% for(var i =0; i < itemList.length; i++){%>
<tr>
<td><%= itemList[i].productId %></td>
<td><%= itemList[i].itemName %></td>
<td><%= itemList[i].category %></td>
<td><%= itemList[i].manufacturer %></td>
<td><%= itemList[i].supplier %></td>
<td><%= itemList[i].buyingPrice %></td>
<td><%= itemList[i].sellingPrice %></td>
<td><input type="text" name="quantity""></td>
<td><input type="checkbox" name="productId" value="<%= itemList[i].productId %>"> | ADD </td>
</tr>
<% } %>
</thread>
</table>
I have tried several things but nothing is working..
Can anyone please give me a solution ?
Modify your code a bit,
//HTML
//add a **onclick** listener on checkbox
<td><input type="checkbox" class="checkbox" name="productId" value="<%= itemList[i].productId %>"> | ADD </td>
//In java script file
$('.checkbox').click(function() {
var productId = $(this).closest("tr").find('td:eq(0)').text();
//changing td:eq(x) will give you corresponding clicked row's cell
}
I tried with the following way, and works well.
Modify the code snippet by adding class name to :
<% for(var i =0; i < itemList.length; i++){%>
<tr class="tbl-item">
...
<% } %>
And implement the click function:
$(".checkbox").each(function(index) {
$(this).click(function(e) {
let item = $(".tbl-item")[index];
console.log(item);
}) ;
})
Then the console will show the desired tag data and you can get every item like:
item.children[4].innerText
Thanks.

What is the equivalent to PHP's print_r() in C#?

I am unable to debug my post submitted data in browser like we do in PHP function:
print_r($_POST);
I'm going to assume you don't need a pretty solution (I think you'd use this for anything other than debugging). Then you can simply do this:
Webforms
<h1>Posted values</h1>
<table>
<% foreach (string key in Request.Form.Keys) { %>
<tr>
<td><%= key %></td>
<td><%= Request.Form[key] %></td>
</tr>
<% } %>
</table>
MVC (put in view file)
<h1>Posted values</h1>
<table>
#foreach (var key in Request.Form.AllKeys) {
<tr>
<td>#key</td>
<td>#Request.Form[key] %></td>
</tr>
}
</table>
I hope that helps.

Installing authlogic 3.4.3 in Rails 4.1.7

Since yesterday I have been trying to install authlogic in a project. As I can see, all the examples and the documentation are oriented to older versions of both rails and authlogic. However, I tried to follow :
the example in the documentation
the railscast
other websites examples like: http://www.logansbailey.com/2010/10/06/how-to-setup-authlogic-in-rails-3/ (for me, the most useful tutorial until now)
I don't get authlogic working properly.
I installed the gem trough the gemfile and the bundle install order.
I generated a model user and ran the migration
$ rails generate scaffold user username:string email:string crypted_password:string password_salt:string persistence_token:string
$ rake db:migrate
I add the following line to the model user.rb:
acts_as_authentic
then, I put a link to register a new user: link_to "Register", new_user_path
I edit /app/views/users/index.html.erb to look like:
<h1>Listing users</h1>
<table>
<tr>
<th>Username</th>
<th>Email</th>
<th></th>
<th></th>
<th></th>
</tr>
<% #users.each do |user| %>
<tr>
<td><%= user.username %></td>
<td><%= user.email %></td>
<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<%= link_to 'New User', new_user_path %>
and app/views/users/_form.html.erb to look like:
<%= form_for(#user) do |f| %>
<% if #user.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(#user.errors.count, "error") %> prohibited this user from being saved:</h2>
<ul>
<% #user.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :username %><br />
<%= f.text_field :username %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit %>
</div>
<% end %>
Now, when I try to register an user, I get two errors:
-Password is too short (minimum is 4 characters)
-Password confirmation is too short (minimum is 4 characters)
I have put longer passwords but it doesn't work.
Any help?
Thank you
Just based on the error messages without seeing the users controller, it appears that password and password_confirmation are not being passed to the user model. This may be a Rails 4 preventing mass assignment issue. To allow this, in the create method in the users controller, you can say:
#user = User.new(user_params)
and then define the user_params method:
def user_params
params.require(:user).permit(:password, :password_confirmation, :name, :email)
end

Resources