Collections query with find method and render to ejs - node.js

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>

Related

ExpressJS : SQLITE_RANGE: column index out of range WHERE condition

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

Nodejs - MongoDB Query

I have a little problem with my Thesis project.
I have a mongodb database with abour 400k entries styled like this:(i've translated the variables so if theres any typo, sorry);
var mongoose = require("mongoose");
var work_schema = new mongoose.Schema({
worktype: String,
ordernumber: String,
art_code: String,
qta: String,
number_of_operatos: String,
1_op_code: String,
2_op_code: String,
3_op_code: String,
4_op_code: String,
phase: String,
notes: String,
continued: String, //just a flag
date_start: String, // DD-MM-YYYY
date_end: String, // DD-MM-YYYY
time_start: String, //HH:MM:SS
time_end: String, //HH:MM:SS
datainiziopart: String, //this is the date parsed like this YYYYMMDD i needed it for another purpose
datafinepart: String, //this is the date parsed like this YYYYMMDD i needed it for another purpose
cronsec: String,
cronsec1: String,
cronsec2: String,
cronsec3: String,
cronsec4: String,
cronsec5: String,
cronsec6: String,
cronsec7: String,
operationtimesec: String,
designtimesec: String,
totalecausalisec: String,
ke: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String,
codicereparto: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "Reparti"
},
nomereparto: String,
descrizionereparto: String
}
}
});
module.exports = mongoose.model("Work", work_schema);
As you can see the schema is not small at all, and having more than 400k entries, i need to query them when exporting/showing.
I used the limit function to show the latest entries.
I want to create a Page with a datepicker to query the data, or using the
date_start: String, // DD-MM-YYYY
or using the
datainiziopart // YYYYMMDD like <>=
Can you help me write the nodejs code to render a page with the result?
I use express if that can help.
i tried to do this
router.get("/risultati", function(req, res) {
Request.getAllRequestListingsCb(function (err, docs){
res.render('pannello/risultati', { rL: docs });
});
});
router.post("/ricercautente", function(req, res) {
var data = req.body.filtro;
var datadivisa = data.split(' - ');
var datestart= moment(datadivisa[0], 'DD-MM-YYYY').format('DD-MM-YYYY');
var dateend= moment(datadivisa[1], 'DD-MM-YYYY').format('DD-MM-YYYY');
//res.redirect("/pannello/utentetutti");
module.exports.getAllRequestListings = function (callback) {
var query = {"datainizio": {"$gte": new Date(datainizio), "$lt": new Date(datafine)}};
Lavori.find(query, callback);
};
res.redirect('pannello/risultati');
});
<div class="" align=center>
<form action="/ricercautente" method="post">
<input type="text" name="filtro" id="filtro" />
<button class="pure-button pure-button-warning" name="" id="" type="submit">Submit</button>
</form>
</div
and on another page
<tbody>
<% lavoritrovati.forEach(function(lavoro){ %>
<tr>
<td> <%=lavoro.author.codicereparto.nomereparto%> </td>
<td> <%=lavoro.tipodilavoro%> </td>
<td> <%=lavoro.ordineproduzione %> </td>
<td> <%=lavoro.codicearticolo %> </td>
<td> <%=lavoro.quantita %> </td>
<td> <%=lavoro.noperatori %> </td>
<td> <%=lavoro.codoperatori%> </td>
<td> <%=lavoro.codoperatori2%> </td>
<td> <%=lavoro.codoperatori3%> </td>
<td> <%=lavoro.codoperatori4%> </td>
<td> <%=lavoro.fase %> </td>
<td> <%=lavoro.note %> </td>
<td> <%=lavoro.datainizio %> </td>
<td> <%=lavoro.timestart %> </td>
<td> <%=lavoro.datafine %> </td>
<td> <%=lavoro.timeend %> </td>
<td> <%=lavoro.continuato %> </td>
<td> <%=lavoro.cronsec %> </td>
<td> <%=lavoro.cronsec1 %> </td>
<td> <%=lavoro.cronsec2 %> </td>
<td> <%=lavoro.cronsec3 %> </td>
<td> <%=lavoro.cronsec4 %> </td>
<td> <%=lavoro.cronsec5 %> </td>
<td> <%=lavoro.cronsec6 %> </td>
<td> <%=lavoro.cronsec7 %> </td>
<td> <%=lavoro.designtimesec %> </td>
<td> <%=lavoro.operationtimesec %> </td>
<td> <%=lavoro.ke %> </td>
<% }); %>

Bootstrap table search and pagination is not work properly

I'm using Node.js with bootstrap 4 and to display data in ejs file, I used bootstrap table from this link.
https://datatables.net/examples/styling/bootstrap4.html
The Issue is then I put static data in table it works fine. But when I fetch from database(MySql) and going to print data. Data print's fine but search and pagination not active/display.
When page load I have set this code to active it.
<script>
$(document).ready(function () {
$('#example').DataTable({
"paging": true,
"ordering": true,
"info": false
});
});
</script>
And in table I print data in this way.
<tbody>
<% if(data.length > 0) { %>
<% for(var i = 0 ; i < data.length ; i++){ %>
<tr>
<td>
<%= i+1 %>
</td>
<td>
<%= data[i].customer_name %>
</td>
</tr>
<% } %>
<% } %>
</tbody>
As I look into your code, Every thing looks good. I have a sample working code in same situation. It may help you.
<table id="example" class="table table-striped table-bordered" style="width:100%">
<thead>
<tr>
<th>Index</th>
<th>Your_header_name</th>
</tr>
</thead>
<tbody>
<% if(data.length > 0) { %>
<% for(var i = 0 ; i < data.length ; i++){ %>
<tr>
<td>
<%= i+1 %>
</td>
<td>
<%= data[i].filed_name %>
</td>
</tr>
<% } %>
<% } %>
</tbody>
</table>

V-Model won't update

I have made a simple Datatable with search input,
Student.js
Vue.component('student-list',{
props: ['students'],
template:`
<div class="table-responsive">
<table class="table table-hover mails m-0 table table-actions-bar">
<thead>
<tr>
<th>10</th>
<th>9</th>
<th>8</th>
<th>7</th>
<th>6</th>
<th>5</th>
<th>4</th>
<th>3</th>
<th>2</th>
<th>1</th>
</tr>
</thead>
<tbody>
<student v-for="student in searchResults" :student="student"></student>
</tbody>
</table>
</div>
`,
computed:
{
searchResults: function(student){
var self = this;
return this.students.filter(
function(student){
if(this.searchQuery == '' || this.searchQuery == null) { return true; }
console.log(this.searchQuery);
return (student.toString().indexOf(this.searchQuery) != -1);
}
);
}
},
// v-if="searchResults(student)"
});
Vue.component('student',{
props: ['student'],
template:`
<tr>
<td>
{{ student.name }} {{ student.lname }}
</td>
<td>
<i class="md-phone"></i>
<i class="ion-woman"></i>
<i class="ion-man"></i>
<i class="ion-android-settings"></i>
</td>
<td>
{{ student.address }}
</td>
<td>
{{ student.totalTopay }}
</td>
<td>
{{ student.lessonsTaken }}
</td>
<td>
{{ student.cancelCount }}
</td>
<td>
{{ student.phone }}
</td>
<td>
{{ student.momPhone }}
</td>
<td>
{{ student.dadPhone }}
</td>
<td>
{{ student.email }}
</td>
</tr>
`
});
new Vue({
el: '#content-page',
data: {
'searchQuery': ''
}
});
HTML
....
<div id="content-page">
<input type="text" id="search" class="form-control" placeholder="Search..." v-model="searchQuery">
</div>
....
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="{{ asset('js/students.js') }}"></script>
...
Now, for some reason whenever I leave the search blank it works as needed, but when I change the input field by typing in the text box(which his v-model is attached to the searchQuery variable), nothing changes, and when I call it (this.searchQuery) on the Dev Tools console, it says undefined. What am I missing?
Thanks in advance everyone!
I've edited your code a little bit to make it work and removed some lines of code, because I don't want to recreate the whole student object.
I passed the search-query as prop and added <student-list /> to your template.
In your filter function I replaced toString with JSON.stringify, because student is an object and toString prints out [object Object].
Also, I changed the props object and added their types.
And one last tiny error:
Interpolation inside attributes has been deprecated. Use v-bind or the
colon shorthand instead.
Don't use href="tel:{{ student.phone }}" use something like this instead :href="'tel:' + student.phone".
Vue.component('student-list',{
props: {
students: {
type: Array
},
searchQuery: {
type: String
}
},
template:`<div class="table-responsive">
searchQuery: {{searchQuery}}
<table class="table table-hover mails m-0 table table-actions-bar">
<thead>
<tr>
<th>10</th>
<th>9</th>
<th>8</th>
<th>7</th>
<th>6</th>
<th>5</th>
<th>4</th>
<th>3</th>
<th>2</th>
<th>1</th>
</tr>
</thead>
<student v-for="student in searchResults" :student="student"></student>
</tbody>
</table>
</div>`,
computed: {
searchResults() {
return this.students.filter(student => JSON.stringify(student).indexOf(this.searchQuery) != -1);
}
},
});
Vue.component('student',{
props: {
student: {
type: Object
}
},
template:`<tr>
<td>
{{ student.name }} {{ student.lname }}
</td>
<td>
<a :href="'tel:' + student.phone" title="התקשר" class="table-action-btn">tel-icon</a>
</td>
</tr>`
});
new Vue({
el: '#content-page',
data: {
'searchQuery': ''
}
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.0.3/vue.js"></script>
</head>
<body>
<div id="content-page">
<student-list :search-query="searchQuery" :students="[{
id: 1,
name: 'Peter',
lname: 'Parker',
phone: 12345
},{
id: 2,
name: 'Bat',
lname: 'Man',
phone: 1234
}]"></student-list>
<input type="text" id="search" class="form-control" placeholder="Search..." v-model="searchQuery">
</div>
</body>
</html>
And my second solution
Add this to your computed:
computed: {
searchQuery() {
return this.$parent.searchQuery || '';
},
...
}
...
You access the parent of student-list, but I think this is not a nice way, because you can't see from where do you get this data.
Hope this will help you a little bit.

How to display specific data in ejs from mongoose?

I am new to NodeJS and MongoDB .I want to specific data from my database using mongoose . Those specific data are decided by a searchIndex and searchValue which is provided as a post request
The ejs page which provides the searchIndex and searchValue
<form action="/searchDetails" method="post">
<table>
<tr>
<td>
Select Search Category:
</td>
<td>
<select name="searchIndex">
<option>Name</option>
<option>Rollno</option>
<option>Branch</option>
<option>Hostel</option>
<option>RoomNo</option>
<option>ContactNo</option>
<option>Semester</option>
<option>FName</option>
<option>MName</option>
<option>Address</option>
</select>
</td>
</tr>
<tr>
<td>Enter the Search Value:</td>
<td><input type="text" name="searchValue"></td>
</tr>
<tr>
<td><input type="Submit" name="Search"></td>
</tr>
</table>
</form>
This is my mongoose schema
var mongoose = require('mongoose');
var userSchema = new mongoose.Schema({
username: String,
password: String,
});
var studentSchema=new mongoose.Schema({
Name:String,
Rollno:String,
Branch:String,
Hostel:String,
RoomNo:String,
ContactNo:String,
Semester:String,
FName :String,
MName:String,
Address:String,
});
mongoose.model('Students3', studentSchema);
mongoose.model('User', userSchema);
this is my server.js
router.post('/searchDetails', function(req, res, next) {
var searchIndex=req.body.searchIndex;
var searchValue=req.body.searchValue;
students.find({searchIndex:searchValue},function(err,data){
if(err)
{
console.log(err);
}
else if(!data)
{
console.log('no data');
res.redirect('/homepage');
}
else
{
console.log(data);
output=data;
res.redirect('/editStudents')
router.get('/editStudents',function(req,res,next){
res.render('editStudents',{output});
});
}
});
});
this is my ejs where i display the value
<table border="1">
<tr>
<td>
<i>Name</i>
</td>
<td>
<i>Rollno</i>
</td>
<td>
<i>Branch</i>
</td>
<td>
<i>Hostel</i>
</td>
<td>
<i>RoomNo</i>
</td>
<td>
<i>ContactNo</i>
</td>
<td>
<i>Semester</i>
</td>
<td>
<i>Father's Name</i>
</td><td>
<i>Mother's Name</i>
</td><td>
<i>Address</i>
</td>
</tr>
<%
output.forEach(function(name)
{
%>
<tr>
<td><%=name.Name%></td>
<td><%=name.Rollno%></td>
<td><%=name.Branch%></td>
<td><%=name.Hostel%></td>
<td><%=name.RoomNo%></td>
<td><%=name.ContactNo%></td>
<td><%=name.Semester%></td>
<td><%=name.FName%></td>
<td><%=name.MName%></td>
<td><%=name.Address%></td>
</tr>
<%
})
%>
</table>
But the problem is it doesnot display any data.please help

Resources