in my schema where i have company with sub document PostedJobs of type array
all companies of are passed to user view
Get Company
router.get('/', isLoggedIn , function(req, res, next) {
Company.find({'Creator': req.user.id}).then(function(companies) {
res.render('Company', { "Companies" : companies });
});
});
after getting a specific company i get that company by name which is unique
router.get('/:name' , isLoggedIn , function(req , res , next) {
var name = req.params.name;
Company.findOne({Name : name}).then(function(Company) {
res.render('dashboard',{
"Company" : Company
});
});
});
with this view i want to also pass PostedJob into company view
The Sub document is present in page and you can access it as i access it as following
<% Company.PostedJobs.forEach(function(Job ,index){%>
<tr>
<td><%= index+1 %></td>
<td><%=Job.JobName %></td>
<td><%=Job.JobType %></td>
<td><%=Job.JobLocation %></td>
<td><%=Job.JobSalary %></td>
</tr>
<%}) %>
Related
I'm trying to get all of the collections in the database and send them over to the handlebars.
My current code gets me the array of all collections, but I'm not quite sure how to get the name of it
router.get('/', isLoggedIn, async (req, res) => {
MongoClient.connect(url).then((client) => {
const connect = client.db(databasename);
connect.listCollections().toArray(function (err, collections) {
if (!err) {
console.log(collections)
res.render('index', { title: 'Home', data: collections });
}
});
});
{{#each data}}
<a href="" class="d-flex">
<i class="my-auto fa-regular fa-copy"></i>
<span class="ms-2"> {{ name }}</span>
</a>
{{/each}}
So basically, I need to get the name of all of the collections and have them in .hbs
for getting the name of all of the collections from MongoDB you can use
db.getCollectionNames()
To know more about this, you can prefer
MongoDB Documentation click on this link
enter link description here
Im new about the Mongoose. So I'm trying to do collections table.
I've this type of Schema for operators:
const operatorSchema = new Schema({
operatorName: {
type: String
},
productsSerialNumber:[{productSerialNumbers: String}],
operatorSerialNumber: {
type: Number
},
users:[{
email:String,
payment:Number,
paymentsData: Date,
}],
operatorState:{
type: Boolean,
}
});
I want to create table like this,
users using some products and I want to list which user using this product also where and when.
As below lines:
OperatorName|productsSerialNumber|username|mail|Date|Payment
But in nodejs side, I couldnt success because users is an Array and how can I get every line from database.
I'm trying like this:
const operatorArray = []
const userArray = []
exports.getOperatorInformations = (req, res, next)=>{
Operators.find({},function(err,docs){
docs.forEach(function(dataCatch){
console.log(dataCatch)
operatorArray.push(dataCatch)
res.render('home',{operators: operatorArray });
})
})
}
}
operatorArray includes all informations, so I send to ejs side:
<% operators.forEach(function(data,index){ %>
<%console.log(index)%>
<tr>
<td>
<%= data.operatorName %>
</td>
<td>
<%= data.productsSerialNumber %>
</td>
<td>
<%= data.productsSerialNumber %>
</td>
<td>
<%= data.users[index].email %>
</td>
<td>
<%= data.users[index].paymentsData %>
</td>
<td>
<!-- <%= data.users[index].payment %> -->
</td>
</tr>
<% })%>
index return length of the operators object. But I want to see lenght of user, when I try with operators.users.forEach It doesnt work. How can I do this ?
You could return the docs array directly to the ejs:
exports.getOperatorInformations = (req, res, next) => {
Operators.find({}, function (err, docs) {
if (err) console.log(err)
else res.render('home', { operators: docs });
});
};
And change your ejs to:
<% operators.forEach(function(data){ %>
<% data.users.forEach(function(user){ %>
<tr>
<td><%= data.operatorName %></td>
<td><%= data.productsSerialNumber %></td>
<td><%= data.productsSerialNumber %></td>
<td><%= user.email %></td>
<td><%= user.paymentsData %></td>
<td><!-- <%= user.payment %> --></td>
</tr>
<% })%>
<% })%>
I am struggling to get records where the name matches the name i have stored an another database.
I can retrieve all fields of data but i am struggling to get specific records i.e. where name = either jack or jill.
I want to use a variable i take from my passport log in which i can access at req.user.username.
I tried using findOne and manipulating the ejs array without any luck.
It prints to the console but i cannot display on vieworders.ejs
I think i am overcomplicating, what is the best way to find and display records where applicationDB.name = req.user.username?
server.js is
app.get('/vieworders', require('connect-ensure-login').ensureLoggedIn(), function(req, res) {
Myapplication.find(function(err, myorders) {
res.render('vieworders', { user: req.user, myorders: myorders});
console.log(myorders);
});
});
vieworders.ejs is
<h1> Display Orders Here </h1>
<% if(myorders.length>0) { %>
<table border="1">
<tr>
<th>Index</th>
<th>Name</th>
<th>size</th>
</tr>
<% for(var i=0; i<hurleys.length;i++) { %>
<tr>
<td><%= i %></td>
<td><%= myorders[i].name %></td>
<td><%= myorders[i].orderssize %></td>
</tr>
<% } %>
</table>
<% } else { %>
No Record Found
<% } %>
MyApplication schema is
const mySchema= new Schema({
name: {
type: String,
required: true,
},
ordersize: {
type: String,
required: true,
},
});
module.exports = mongoose.model('MyApplication', mySchema);
You have to pass the query filter as the first parameter of the find method.
This should do the trick, assuming the username is stored in the Schema as name.
app.get('/vieworders', require('connect-ensure-login').ensureLoggedIn(), function(req, res) {
Myapplication.find({name: req.user.username}, function(err, myorders) {
res.render('vieworders', { user: req.user, myorders: myorders});
console.log(myorders);
});
});
I am using the Ejs templating engine for my expressjs project and despite passing my objects along to my view blog.ejs file, I am receiving an blogpost not defined error in my ejs file. Error is happening at my <% blogpost.forEach(function(blogpost) { %> line. I figure that is has something to do with how im passing the object and its properties, but I followed guides and it appears correct.
routes.js:
//blog
router.route('/blog')
// START POST method
.post(function(req, res) {
var blogpost = new Blogpost(); // create a new instance of a Blogpost model
blogpost.title = req.body.title; // set the blog title
blogpost.author = req.body.author; // set the author name
blogpost.content = req.body.content; // set the blog content
blogpost.date = req.body.date; // set the date of the post
//Save Blog Post
blogpost.save(function(err) {
if (err)
res.send(err);
res.json({ message: 'Blog created.' });
});
}) // END POST method
// START GET method
.get(function(req, res) {
Blogpost.find(function(err, blogpost) {
if (err)
res.send(err);
blogpost.title = req.body.title; // update the blog title
blogpost.author = req.body.author; // set the author name
blogpost.content = req.body.content; // update the blog content
blogpost.date = req.body.date; // set the date of the post
res.render('pages/blog', {
title: blogpost.title,
author: blogpost.author,
content: blogpost.content,
date: blogpost.date
});
});
}); // END GET method
blog.ejs:
<html>
<head>
<% include ../partials/head %>
</head>
<body>
<header>
<% include ../partials/header %>
</header>
<div class="grid">
<div class="col-1-1">
<div class="body-content">
<% blogpost.forEach(function(blogpost) { %>
<h1><%= blogpost.title %></h1>
<% }); %>
</div>
</div>
</div>
<footer>
<% include ../partials/footer %>
</footer>
</body>
</html>
You're not passing an array variable called blogpost to your template, you are instead passing these variables to your template:
title: blogpost.title,
author: blogpost.author,
content: blogpost.content,
date: blogpost.date
You could just do this render() instead of the one you currently have:
res.render('pages/blog', {
blogpost: blogpost,
});
I am trying to display my mongodb data in a bootstrap template.
api.js
var Software = require('../models/dbSoftware.js');
exports.show = (function(req, res) {
Software.find({Publisher: req.params.format}, function(error, softwares) {
res.render('searchpage', { searchText: req.params.format, softwares: softwares});
})
});
Software Schema:
var mongoose = require('mongoose');
var softwareSchema = new mongoose.Schema({
SoftwareName: String
, Size: String
, Publisher: String
, Product: String
, Version: String
, Language: String
, License_Type: String
, Description: String
, License_Key: String
});
module.exports = mongoose.model('Software', softwareSchema);
searchpage.ejs
<td><%= softwares.Product %></td>
This returns undefined value. Any softwares.* returns 'undefined'.
When I edit searchpage.ejs to:
<td><%= softwares%></td>
The output is:
{ License_Key: 'computing', Description: 'Licensed', License_Type: 'English', Language: 'UNKNOWN', Product: 'Test Test', Publisher: 'Test', Size: '231.6MB', SoftwareName: 'Test.zip', _id: 5252c407d9b28d1e4c000001, __v: 0 }
//which is correct
When using .find(), softwares will be an Array for all successful (error == null) queries, regardless of the number of documents found:
<%= softwares.length %>
<%= softwares[0].Product %>
If you only want a single document, you'll want to use .findOne() instead:
Software.findOne({Publisher: req.params.format}, function(error, softwares) {
// ...
});
<% if (softwares != null) { %>
<%= softwares.Product %>
<% } %>
Or, you can iterate over softwares:
<% softwares.forEach(function (software) { %>
<%= software.Product %>
<% }) %>