How can i fetch the buffer image from mongodb on ejb page - node.js

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...

Related

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

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}` %>">

How to use "Flat Json"-Bootstrap 5 Table example with ejs + nodejs + expressjs

I want to use the "Flat Json"-Bootstrap 5 Table example. [Link][1]
I am using expressjs with nodejs and ejs.
My Code so far:
```<link href="https://unpkg.com/bootstrap-table#1.18.1/dist/bootstrap-table.min.css" rel="stylesheet">
<script src="https://unpkg.com/bootstrap-table#1.18.1/dist/bootstrap-table.min.js"></script>
<body>
<h2>JOB DASHBOARD</h2>
<table id="table" data-toggle="table" data-flat="true" data-search="true" >
<thead>
<tr>
<th data-field="" data-sortable="true">Order ID</th>
<th data-field="" data-sortable="true">Liefertermin</th>
<th data-field="" data-sortable="true">Fertigstellungstermin</th>
<th data-field="" data-sortable="true">Keyline Nr</th>
</tr>
</thead>
<tbody>
<% var count = 0; %>
<% orders.forEach((order)=>{ %>
<tr data-index="<%= count %>">
<td><%= order.order_id %></td>
<td><%= order.due_at %> </td>
<td><%= order.deliver_at %> </td>
<td><%= order.keyline_obj.event.data.attributes.reference %> </td>
</tr>
<% count +=1; %>
<% }) %>
</tbody>
</table>
</body>
</html>```
I am loading the content from my mongoDB Database. The Variable "orders" is an array of order documents.
At the moment the data is loading but the searchfield doesnt appear at all and all the styling doesnt work although I have loaded the bootsrap css and js.
[1]: https://examples.bootstrap-table.com/index.html?bootstrap5#welcomes/flat-json.html
i think the problem is that the is successfully sent but the respond is in json as the browser waiting
you can check data is received or not by => clicking right click on the screen =>press inspect => Network => click on the page file => then choose Response
you will find your html page with the updated data in it
you can render it from your js script after fetching api like so
await fetch(----your fetching code-----).then(res=>{
document.open();
res.text().then(function(text) {
document.write(text)
});
})

Cannot Get POST Route

I am working on a project but unable to find the bug i have made in the following code.
I dont find any issues (imo) in the following code but somehow it's not posting requests.
Im unable to receive post requests on this route:
Error: Cannot POST /campgrounds/5c023e5f1452761b2b937e91/maketreasurer
router.post("/:id/adminpanel/maketreasurer",function(req,res){
res.send("posted");
});
Im sending my request from this page:
<% include ../partials/header %>
//show all faculty heads
//select faculty header
//assign faculty head
<h3>Users</h3>
<table id="t01">
<tr>
<th>firstname</th>
<th>----</th>
<th>Username</th>
<th>----</th>
<th>Faculty Number</th>
<th>----</th>
<th>Student Number</th>
<th></th>
</tr>
<% parm.users.forEach(function(users){ %>
<tr>
<td><%= users.firstname %></td>
<td></td>
<td><%= users.username%></td>
<td></td>
<td><%= users.facultynumber%></td>
<td></td>
<td><%= users.studentnumber%></td>
</tr>
<% }); %>
</table>
</form>
Make A Treasurer
<form action="/campgrounds/<%=parm.id%>/maketreasurer"
method="POST">
<button>Submit</button>
</form>
The error is pretty obvious,
Cannot POST `/campgrounds/5c023e5f1452761b2b937e91/maketreasurer`
where id = 5c023e5f1452761b2b937e91
Now, you're route is /:id/adminpanel/maketreasurer considering /campgrounds as base url, where is adminpanel? The post request should have been:
/campgrounds/5c023e5f1452761b2b937e91/adminpanel/maketreasurer

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.

Resources