I am working on a project using express , EJS and SQLite , So i made a function that select data from the database, and render it to the interface, this is the function:
function findHomeforSell(req , res , next){
const sell = "Sell";
db.all('SELECT * FROM home WHERE for = ? ', [
sell
] ,function(err , rows){
if (err) { return next(err);}
var homes = rows.map(function(row) {
return {
id: row.id,
location: row.Location,
four: row.for,
space: row.space,
bathrooms: row.bathrooms,
bedrooms: row.bedrooms,
price: row.price,
url: '/' + row.id
}
});
res.locals.homes = homes;
});
};
the ejs file:
<div class="house-list" id="buy">
<h1>Buy</h1>
<ul class="todo-list">
<% homes.forEach(function(home) { %>
<li>
<form action="<%= home.url %>" method="post">
<div class="view">
<table>
<thead>
<th>Location</th>
<th>For</th>
<th>Space</th>
<th>Bathroom</th>
<th>Bedroom</th>
<th>Price</th>
</thead>
<tbody>
<td><label ondblclick=""><%= home.location %> </label></td>
<td><label ondblclick=""> <%= home.four %></label></td>
<td><label ondblclick=""><%= home.space %> SQRM </label></td>
<td><label ondblclick=""> <%= home.bathrooms %></label></td>
<td><label ondblclick=""><%= home.bedrooms %> </label></td>
<td><label ondblclick="">$ <%= home.price %> SDG</label></td>
</tbody>
</table>
</div>
</li>
<% }); %>
</ul>
</div>
and the error:
SQLITE_RANGE: column index out of range
I couldn't understand the error so what to do
Related
This the code of sending an email using mailgun:
const qrcodeEmail = async (email, subject,text,qrcode,user,data) => {
try {
const requiredPath = path.join(__dirname, "../views/emailTemplates/qrcodeEmail.ejs");
const emailData = await ejs.renderFile(requiredPath,{user:user,data:data,qrcode:qrcode});
var mailData = {
from: 'moilasurprise7#gmail.com',
to: 'moilasurprise7#gmail.com',
subject:subject,
html:emailData,
};
mailgun.messages().send(mailData, function (error, body) {
if(error){
console.log(error);
}
console.log("email sent successfully");
});
} catch (error) {
console.log(error.message);
}
};
The email is sent,the user and order details are showing but the qr-code is not displaying down here is the ejs email template.
Note: the qr-code is generated in this manner.
await QRCode.toDataURL(text);
This is the ejs template where the qrcode is rendered:
<table style="width:100%;max-width:620px;margin:0 auto;background-color:#ffffff;">
<tbody>
<tr>
<td style="padding: 30px 30px 20px">
<p style="margin-bottom: 10px;">Hi <%= user.name %> <%=user.lastName%> ,</p>
<p style="margin-bottom: 10px;">Here is your ticket order</p>
<p style="margin-bottom: 10px;">eventName : <%= data.eventName %> </p>
<p style="margin-bottom: 10px;">EventVenue : <%= data.eventVenue %> </p>
<p style="margin-bottom: 10px;">eventDuration : <%= data.eventDuration %> </p>
<p style="margin-bottom: 10px;">eventRegion : <%= data.eventRegion %> </p>
<p style="margin-bottom: 10px;">Ticket Type : <%= data.category %> </p>
<p style="margin-bottom: 10px;">Ticket price : $<%= data.price %> </p>
<p style="margin-bottom: 10px;">eventDate: <%= data.eventDate.getDate()%>/<%= data.eventDate.getMonth() + 1%>/<%= data.eventDate.getFullYear()%> </p>
<img src="<%=qrcode%>" alt="qr-code">
</td>
</tr>
</tbody>
</table>
I have operator Collection in mongoDB. And I want to get all information in ejs table so I wrote below codes. But I get error with operators.forEach is not a functions How can I solve this problem ?
This is Collection model:
const operatorSchema = new Schema({
operatorName: {
type: String
},
productsSerialNumber:[{productSerialNumbers: String}],
operatorSerialNumber: {
type: Number
},
users:[{
email:String,
payment:Number,
paymentsData: Date,
}],
operatorState:{
type: Boolean,
}
});
And this is the query function:
exports.getOperatorInformations = (req, res, next)=>{
Operators.find({},function(err,docs){
docs.forEach(function(dataCatch){
console.log(dataCatch.users)
res.render('home',{operators: dataCatch});
})
})
}
}
And ejs file part:
<% operators.forEach(function(data){ %>
<tr>
<td>
<%= data.operatorName %>
</td>
<td>
<%= data.productsSerialNumber %>
</td>
<td>
<%= data.productsSerialNumber %>
</td>
<% })%>
<% data.users.forEach(function(data){ %>
<td>
<%= data.users.email %>
</td>
<td>
<%= data.users.paymentsData %>
</td>
<td>
<!-- <%= data.users.payment %> -->
</td>
</tr>
<% })%>
</tr>
I have an application nodejs + expressjs + ejs.
I am building a history search screen, my question is how to update my ejs html table with the values returned in the response.
Uploading the screen with the values is working, but when I enter the filters and search, I would like to update my table.
In the example below is how to use to load the html the first time with the data of the query, I would like to fill in an initial and final date and click on searching and updating the html.
EJS HTML
<div class="row">
<div class="col-md-2">
<div class="form-group">
<label for="dataPrimayID">Date Primary</label>
<input type="text" class="form-control datepickerDefault" id="dataPrimayID" name="dataPrimary" maxlength="250">
</div>
</div>
<div class="col-md-2">
<div class="form-group">
<label for="dataFinalID">Date Final</label>
<input type="text" class="form-control datepickerDefault" id="dataFinalID" name="dataFinal" maxlength="250">
</div>
</div>
<div class="col-md-2">
<a id="btnSearch" class="btn btn-primary">Search</a>
</div>
</div>
<div class="row">
<div class="table-responsive col-md-12">
<table id="dataTableHistoricoID" class="table table-striped" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th>Name</th>
<th>LastName</th>
</tr>
</thead>
<tbody>
<% if(data.length > 0){ %>
<% for ( var i = 0 ; i < data.length ; i++){ %>
<tr>
<td>
<%= data[i].name%>
</td>
<td>
<%= data[i].lastname%>
</td>
</tr>
<% } %>
<% } %>
</tbody>
</table>
</div>
</div>
ROUTER
var express = require('express');
var router = express.Router();
var historyController = require('../controllers/history-controller');
router.get('/find', historyController.findHistory);
module.exports = router;
Controller
db.query(sql, function (err, rows) {
if (err) {
var message = err.message;
console.log(message);
return res.status(500).send(message);
}else {
var data = rows;
res.render('history/search-history', {data: data});
}
});
EJS is static after compiling, you can't change it on backend-side. There are two ways of it.
Refreshing the page every time you want to render new content.
Implement client-side JS code that will handle api requests and render new elements.
For example, using Jquery:
$.post(url, (result) => {
let data = JSON.parse(result);
let innerHTML = '';
for (let item of data) {
innerHTML += `<tr>
<td>
${item.name}
</td>
<td>
${item.lastname}
</td>
</tr>`;
}
$('#dataTableHistoricoID tbody').html(innerHTML)
};
I should Have got the else part but I am getting the error: Expected end tag for cart.items but it was not found. At line : 21, column : 35. I don't know where is the problem everything is fine may be indentation error please look out the code and suggest me. thanks there is also attached screen shot of the error.
this is my dust file:
{>"layouts/master" /}
{<body}
<div class="row">
<div class="container">
<h2>Your Cart</h2>
{?cart.items}
<table>
<thead>
<tr>
<th>Item</th>
<th>Quantity</th>
<th>Total</th>
<th>Action</th>
</tr>
</thead>
<tbody>
{#cart.items}
<tr>
<td>{title}</td>
<td>{.qty}</td>
<td> ${#math key="{.price}" method="multiply" operand ="{.qty}"}</td>
<td>Remove</td>
</tr>
{/cart.items}
</tbody>
</table>
<div class="row">
<div class="col-sm-6">
<h4> Total <strong>${cart.total}</strong></h4>
</div>
<div class="col-sm-6">
</div>
</div>
{:else}
<p>There is no Items in your cart</p>
{/cart.items}
</div>
</div>
{/body}
this is my controller
'use strict';
var Book = require('../models/bookModel');
var Category = require('../models/categoryModel');
module.exports = function (router) {
router.get('/', function (req, res) {
var cart = req.session.cart;
var displayCart = {
items: [], total: 0
};
var total = 0;
//get total
for(var item in cart){
displayCart.items.push(cart[item]);
total += (cart[item].qty * cart[item].price);
}
displayCart.total = total;
res.render('cart/index', {cart: displayCart});
});
});
I am Getting this error:
The math helper should be closed. E.g:
{#math key="{.price}" method="multiply" operand ="{.qty}" /}
This is my Product method for displaying all the PRODUCTS at the Home PAGE WITH respect to its Categories
//GET Products DEtails
router.get('/:category/:product',function(req,res){
var galleryImages=null;
models.Product.findOne({
where:{slug:req.params.product}
})
.then(function(product){
var galleryDir='public/product_images/' + product.id + '/gallery';
fs.readdir(galleryDir,function(files){
galleryImages=files;
res.render('product',{
title:product.title,
p:product,
galleryImages:galleryImages,
})
})
})
});
This is mine Product.ejs file for displaying it the P.Images is working above but the forEach loop is not working Cannot read property forEach of Null the Path for mine Gallery images is Correct
<% include layouts/header %>
<div class="row">
<h1 class="page-header"><%= p.title %></h1>
<div class="col-xs-12 col-md-5">
<img class="spi" src="/product_images/<%= p.id %>/<%= p.images %>" alt="">
<br>
</div>
<div class="col-xs-12 col-md-7">
<p><%= p.desc %></p>
<p>$<%= parseFloat(p.price).toFixed(2) %></p>
<p>Add To Cart</p>
</div>
<div class="col-xs-12">
<ul class="gallery">
<% galleryImages.forEach(function(images){ %>
<% if (images != "thumbs") { %>
<li>
<!--<a data-fancybox="gallery" href="/product_images/<%= p.id %>/gallery/<%= images %>"> -->
<img src="/product_images/<%= p.id %>/gallery/thumbs/<%= images %>" alt="">
</a>
</li>
<% } %>
<% }); %>
</ul>
</div>
</div>
<!--
- will gave Content Where as = will gave Html <P>tag in the Browser</P> -->
<% include layouts/footer %>
This is my products.js after try. It worked
/*
* GET product details by category
*/
router.get('/:category/:product', function (req,res){
Product.findOne({slug: req.params.product}, function (err, product) {
if(!product) { //if product not exist in db
return res.status(404).send('Product not found');
}else{
var galleryDir = 'public/product_images/' + product._id + '/gallery';
fs.readdir(galleryDir, function (err, files) {
if (err) {
console.log(err);
} else {
galleryImages = files;
res.render('product', {
title: product.title,
p: product,
galleryImages: galleryImages
});
}
});
}
});
});