how to associate data schema in mongoose - node.js

I'm trying to associate 2 schema on nodejs/mongoose app.
my first model:
var mongoose = require("mongoose");
var projectSchema = new Schema({
pname: String,
pnumber: String,
plocation: String,
pclient: String,
clientabn: String,
contname: String,
contnumber: String,
mobile: String,
address: String,
city: String,
country: String,
boqs: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Boq",
}]
});
module.exports = mongoose.model("Project", projectSchema);
second model:
var mongoose = require("mongoose");
var boqSchema = new Schema({
boqDesc: String,
boqUnit: String,
boqQty: String,
boqRate: String,
boqPrice: String,
});
module.exports = mongoose.model("Boq", boqSchema);
And this is my post rout:
app.post("/myprojects/:id/boq", function(req, res) {
Project.findById(req.params.id, function(err, project) {
if (err) {
console.log(err);
} else {
Boq.create(req.body.boq, function(err, boq) {
if (err) {
console.log(err);
} else {
project.boqs.push(boq);
project.save();
res.redirect("/myprojects/" + project._id + "/boq");
console.log(boq);
}
});
}
});
});
When is posting, only an id of boq will be saved on database not any data from boq's form. anyone knows what is wrong with this code?
output of console.log:
console.log output
console.log(project)
Below is the post form:
<form action="/myprojects/<%= project._id %>/boq" method="POST">
<table class="table" id="toptab">
<tbody>
<tr>
<td class="td6"><b>No.</b></td>
<td class="td7"><b>Item Description/Scope of Work</b></td>
<td class="td8"><b>Unit</b></td>
<td class="td9"><b>Quantity</b></td>
<td class="td10"><b>Rate</b></td>
<td class="td11"><b>Price</b></td>
</tr>
<tr>
<td class="td6"></td>
<td class="td7" contenteditable="true" name="boq[boqDesc]"></td>
<td class="td8" contenteditable="true" name="boq[boqUnit]"></td>
<td class="td9" contenteditable="true" name="boq[boqQty]"></td>
<td class="td10" contenteditable="true" name="boq[boqRate]">$</td>
<td class="td11" contenteditable="true" name="boq[boqPrice]"></td>
</tr>
</tbody>
</table>
<button type="submit" name="submit" class="btn btn-primary">Save</button>
</form>

you're calling Boq.create(undefined, callback);
this creates a boq document in db with no fields (except the _id).
it passes validation because all boq's fields are not required.
go to your client, and make sure you're adding boq data to http request's body correctly.
add <input> elements in between <td></td> tags, each named after one of the fields in boq's schema. for instance <td><input type="text" name="boqDesc"></td>

Related

Mongoose Collections List Method

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>
<% })%>
<% })%>

Search array MongoDB NodeJS

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);
});
});

Handlebars: Access has been denied to resolve the property "name" because it is not an "own property" of its parent

I got the issue with handlebars 4.7.3. I already checked the solution from this ask,
Handlebars: Access has been denied to resolve the property "from" because it is not an "own property" of its parent
but it was no solution for my code so I hope someone can help me.
Controller.js
submitPosts: (req, res) => {
// Check the attributs from create.handlebars for success or error message
const newPost = new Post( {
surname: req.body.surname,
name: req.body.name,
biography: req.body.biography,
profilpicture: req.body.profilpicture,
paintings: req.body.paintings,
});
// Safe new posts
newPost.save().then(post => {
console.log(post);
flash('success-message', 'new post!')
res.redirect('/admin/posts');
});
},
postModel.js
const
mongoose = require('mongoose');
const Schema = mongoose.Schema; // get props
const PostSchema = new Schema({
// define props --> required for a post
surname: {
type: String,
default: ''
},
name: {
type: String,
default: ''
},
biography: {
type: String,
default: ''
},
profilpicture: {
type: Object,
},
paintings : {
type: Object,
}
});
module.exports = mongoose.model('post', PostSchema);
index.handlebars
{{#each post}}
<tr>
<td>{{ surname }}</td>
<td>{{ name }}</td>
<td><img style="width: 100px; height:100px;" src="{{ profilpicture }}"></td>
<td>{{ biography }}</td>
<td><img style="width: 100px; height:100px;" src="{{ paintings }}"></td>
</tr>
{{/each}}
Already tried every possibility from the other ask on stack overflow, other handlebars version, change router code, ... NOTHING WORKS :(
Just had the same issue that wrecked my nerves.
Tried additional packages etc but in the end a simple command solve this.
Its the Command ".lean()"
Found more here: link to mongoose docs for lean()
my code example:
// show message page Route
router.get("/", (req, res) => {
//read all Message entries from db
Message.find()
//sort them descending by date property
.sort({ date: "desc" })
.lean()
//take these and call the rendered page and pass them as object to the page
.then(messages => {
res.render("messages/show_messages", { messages: messages });
});
});

Node.JS, Express & MongoDB Atlas:: Multiple Collections ejs

I have tried almost all the solutions but couldn't get this fixed.
I'm trying to get multiple collection of data (Mongodb Atlas) onto one ejs file. But I can't get data onto the rendered ejs sheet. But when I tested with locus I can see all the data received from the database up to res.render("/vendor/show"). But it is not passing onto ejs template.
I have three sets of data sitting on the mongo Atlas:
const vendorSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
});
const newpromoSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
vendor:String,
description:String,
sdate:String,
edate:String,
});
const neweventSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
description:String,
vendor:String,
sdate:String,
edate:String,
});
router.get("/vendors/:id",function(req,res) {
var events={}; //Create Empty event Object
var promotions={}; //Create Empty promotion Object
var vendors={};//Create Empty vendor Objext
Promotion.find({},function (err,allPromotions) {
if (err) {
console.log(err);
} else {
//Find Collection And Assign It To Object
promotions=allPromotions;
}
});
Event.find({},function(err, allEvents) {
if (err) {
console.log(err);
} else {
events=allEvents;
}
});
Vendor.find({},function(err,allVendors){
if(err){
console.log(err);
}else{
vendors=allVendors;
//find order collection and passing it to ejs templates
res.render("vendors/show",{ event:events, promotion:promotions vendor:vendors});
}
});
});
Show.ejs code as
<%=vendor.name%>
<%=promotion.name%>
<%=event.name%>
You need to handle the async process. In your current code, there are three async processes. Please go through following the link you will definitely get an idea about it.
https://blog.risingstack.com/mastering-async-await-in-nodejs/
if you need more help Don’t hesitate to comment.
I managed to get all data using the below function. But now I have another issue, need to filter all the Promotions and Events by vendor But it doesn't filter.
Data Schema
const neweventSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
description:String,
vendor:String,
sdate:String,
edate:String,
});
const Event = mongoose.model("Event",neweventSchema);
module.exports = Event;
//SCHEMA SETUP===============================================================================================================
const newpromoSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
vendor:String,
description:String,
sdate:String,
edate:String,
});
//compile into a model
const Promotion = mongoose.model('Promotion',newpromoSchema);
module.exports = Promotion;
//SCHEMA SETUP===============================================================================================================
const vendorSchema = new mongoose.Schema({
name:String,
image:String,
imageId:String,
});
const Vendor = mongoose.model("Vendor", vendorSchema);
module.exports = Vendor;
Routes as below
router.get("/vendors/:id", function (req, res) {
Vendor.find({}, function (err, allVendors) {
if (err) {
console.log(err);
} else {
// //Find Collection And Assign It To Object
Event.find({}, function (err, allEvents) {
if (err) {
console.log(err);
} else {
Promotion.find({}, function (err, allPromotions) {
if (err) {
console.log(err);
} else {
//find order collection and passing it to ejs templates
res.render("vendors/show", {event: allEvents, promo: allPromotions, vendor: allVendors});
}
});
}
});
}
});
});
EJS Show page But It doesn't get filtered.
<!-- Trying to filter all the events by vendor if event's vendor name == vendor name then show on the show page -->
<%if(event.vendor === vendor.name){%>
<div class = "container-fluid">
<div class = "row">
<%event.forEach(function(events){%>
<div class="col-sm-6 col col-md-4 col-lg-3">
<div class="thumbnail">
<img src ="</%=events.image%>" class="rounded" width="304" height="236">
<div class="caption">
<h4><%=events.name%></h4>
<p class="font-italic font-weight-bold text-muted">
<%=events.vendor%>
</p>
<p><strong>Start Date</strong><%=events.sdate%></p>
<p><strong>End Date</strong><%=events.edate%></p>
</div>
<p>Event Details <br>
<%=events.description.substring(0,100)%>
</p>
<p>
Find More
</p>
</div>
</div>
<%})%>
</div>
</div>
<%}%>
<!-- //Trying to filter all the promotions by vendor if vendor name= to promotions's vendor name only show in the show page-->
<%if(promo.vendor===vendor.name){%>
<div class = "container-fluid">
<div class = "row">
<%promo.forEach(function(promotions){%>
<div class="col-sm-6 col col-md-4 col-lg-3">
<div class="thumbnail">
<img src ="<%=promotions.image%>" class="rounded" width="304" height="236">
<div class="caption">
<h4><%=promotions.name%></h4>
<p class="font-italic font-weight-bold text-muted"> <%=promotions.vendor%></p>
<p><strong>Start Date</strong> <%=promotions.sdate%></p>
<p><strong>End Date</strong> <%=promotions.edate%></p>
</div>
<p>
<%=promotions.description.substring(0,100)%>
</p>
<p>
Find More
</p>
</div>
</div>
<%})%>
</div>
</div>
<%}%>

Editing text on a page in node js

I am trying to implement a simple edit feature in my app. In my profile.handlebars file, I have an edit button. When clicked, I'd like the user's information to appear in the text input fields on the form in order to allow the user to edit their existing information.
Right now, they would have to input all of their information over again (and every field in the form would need to be filled out due to validation that I have implemented), click Submit, and their profile can be updated. Is this possible without using a framework (like Angular)? For example, in LinkedIn, a user can hover over a section of their profile causing the edit buttons to highlight, then click a single edit button, and they're instantly in editing mode. That might be too advanced for my purposes right now, but eventually, I'd love to have functionality like that.
I have a post request in my routes file to handle a user posting information to their profile:
router.post('/add', function(req, res) {
req.checkBody({
'city': {
errorMessage: 'Please enter your city'
},
'state': {
errorMessage: 'Please enter your state',
notEmpty: true
},
'zip': {
errorMessage: 'Please enter your zip code',
notEmpty: true
},
'about': {
errorMessage: 'Please briefly describe yourself',
notEmpty: true
}
});
console.log("req.user " + req.user);
var errors = req.validationErrors();
if (errors) {
res.render('profile', {
errors: errors
});
} else {
var user_info = new User_Info({
city: req.body.city,
state: req.body.state,
zip: req.body.zip,
about: req.body.about,
user_name: req.user.username
});
user_info.save(function(err, user_info) {
if (err) throw err;
});
res.redirect('profile/' + req.user.username)
}
})
Then, I have my profile.handlebars file:
{{#if errors}}
Uh oh! Something went wrong. Please review the below errors, and try again.<br><br>
<ul>
{{# each errors }}
<li style="color: red">{{this.msg}}</li>
{{/each}}
</ul>
{{else}}
<h3 align="center">Profile ({{user_name.name}})</h3>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="thumbnail" style="border-radius: 12px">
<div class="caption">
<p>City: {{# each information }} {{this.city}} {{/each}}</p>
<p>State: {{# each information }} {{this.state}} {{/each}}</p>
<p>Zip: {{# each information }} {{this.zip}} {{/each}}</p>
<p>About: {{# each information }} {{this.about}} {{/each}}</p>
<div class="btn-group">
<button type="Edit" class="btn btn-danger dropdown-toggle deleteLocation" data-id="{{this.id}}">Edit</button>
</div>
</div>
</div>
</div>
</div>
<br>
<center>
<form method="POST" action="/users/add">
<input type="text" name="city" placeholder="City" style="text-align: left">
<br><br>
<input type="text" name="state" placeholder="State" style="text-align: left">
<br><br>
<input type="text" name="zip" placeholder="Zip" style="text-align: left">
<br><br>
<textarea name="about" placeholder="About You" style="text-align: left; resize: both;" rows="5" cols="50"></textarea>
<br><br>
<div class="btn-group">
<button type="submit" class="btn btn-success dropdown-toggle" aria-haspopup="true" aria-expanded="false">Submit</button>
</div>
<br><br>
</form>
</center>
{{/if}}
Please let me know if you need additional info to help me solve this issue. Thanks!
you can use this code for node for editing the parameters , city,state,zip and about.
router.post('/add', function (req, res) {
var users = req.Collection;
var city = req.body.city;
var state = req.body.state;
var zip = req.body.zip;
var about = req.body.about;
var user_id = req.body.user_id;
if (city && state && ) {
users.findOneAndUpdate({_id: user_id}, {$set: {city: city, state: state, zip: zip, about:about}}, function (err, user) {
if (err) {
res.json({status: 0, message: err});
}
if (!user) {
res.json({status: 0, msg: "not found"});
} else {
res.json({status: 1, city: city, state: state, zip: zip, about:about, message: " edit success"});
}
})
} else {
res.json({status: 0, msg: "Invalid Fields"});
}
});

Resources