In Nodejs , why cant I req object under GET method? - node.js

In nodejs using express 4.x, I receive an error req.query is undefined.
I have a layout.ejs file with a link href="/editprofile?email=<%=user.email%>" to prepare to direct to index.js with the GET method.
But I can not get the req.query object; it displays undefined. Please help, and thanks.
[layout.ejs]
<div class="nav-collapse">
<ul class="nav">
<li class="active">首頁</li>
<%if ( typeof(user) != 'undefined' && user != null) { %>
<li>登出</li>
<li>
<a href="/editprofile?email=<%=user.email%>" ><img src="/images/user.jpg" height="25" width="25">
</a>
</li>
<% } else { %>
<li>登入</li>
<li>註冊</li>
<%} %>
</ul>
</div>
[index.js]
router.get('/editprofile',function(res, req, next){
console.log("req.query.email:"+req.query);
User.get(email,function(err,user){
if(!user){
req.flash('error','無此使用者!');
return res.redirect('/');
}
if(err){
req.flash('error', err);
return res.redirect('/');
}
});
});

Related

Can't get name from req.user.name to output in Nodejs

your text <%= post.user.name %> show TypeError in ejs file
this is my post_controller:-
this is error show in web page
const Post = require('../models/post'); module.exports.home = function (req, res) {
// populate the user of each post
Post.find({}).populate('user').exec(function (err, posts) {
return res.render ('home', {
title: "Home",
posts: posts
});
})
}
This is my home.ejs file part:-
<ul>
<% for(post of posts){ %>
<li>
<%= post.content %>
<br>
<%= post.user.name %>
</li>
<% } %>
</ul>

Cannot read property 'contentType' of undefined

I'm creating facebook like social media app using nodejs and ejs. I'm getting error " Cannot read property 'contentType' of undefined". But I get this error on profile.ejs and not on newsfeed.ejs. profile.ejs render photo if there's photo in the post but if I post a post with text only,this "Cannot read property 'contentType' of undefined" error occur.
app.js
app.get("/newsfeed", function(req, res){
if(req.isAuthenticated()){
Post.find({}, function(err, foundItem){
if(err){
console.log(err);
} else {
const loggedUser = req.user;
res.render("newsfeed", {posts: foundItem, loggedUser: loggedUser, foundUser: "undefined"});
}
}).sort({date: -1});
} else {
res.redirect("/login");
}
});
app.get("/profile/:userProfile", function (req, res) {
const userId = req.params.userProfile;
const loggedUser = req.user
if(req.isAuthenticated){
User.findById(userId, function(err, foundUser){
if(err){
console.log(err);
} else {
res.render("profile", {foundUser: foundUser, loggedUser:loggedUser});
}
}).sort({date: -1});
}
})
newsfeed.ejs
<% posts.forEach( function(post) { %>
<div class="post">
<!-- Post contents -->
<%= post.name %>
<p class="date"><%= post.date %></p>
<p class="content"><%= post.content %></p>
<% if(post.img != "undefined") { %>
<div class="mediaFiles">
<div class="imageOverlay"><i class="fas fa-times"></i></div>
<img src="data:image/<%=post.img.contentType%>;base64,
<%=post.img.data%>" class="image" onerror="this.style.display='none'">
</div>
<% } %>
<hr>
<% }); %>
profile.ejs
<% foundUser.post.forEach( function(post) { %>
<div class="post">
<!-- Post contents -->
<%= post.name %>
<p class="date"><%= post.date %></p>
<p class="content"><%= post.content %></p>
<% if(post.img != "undefined" || post.img != null || post.img != "") { %>
<div class="mediaFiles">
<div class="imageOverlay"><i class="fas fa-times"></i></div>
<img src="data:image/<%=post.img.contentType%>;base64,
<%=post.img.data%>" class="image" onerror="this.style.display='none'">
</div>
<% } else { %>
<div class="imageOverlay"><i class="fas fa-times"></i></div>
<img src="#" class="image" onerror="this.style.display='none'">
</div>
<% } %>

Html datalist option through JavaScript in node js

I want to pass data from MySQL database in datalist option. My application is written in express js using ejs view. I can't figure out how to pass database values to list in JavaScript and how to pass this list to ejs file.
add.js:
module.exports = {
addProductPage: (req, res) => {
let query = "SELECT shipper_names.Shipper_ID, shipper_names.Shipper_Name FROM shipper_names";
conn.query(query, (err, results) => {
if (err) {
return res.status(500).send(err);
}
res.render('add-product.ejs', {
title: "Add Product",
shipper_names: results[0],
message: ''
});
});
}
}
EJS file:
<!doctype html>
<html lang="en">
<div>
<a class="float-right" href="/" title="Home">Home</a>
</div>
<div class="contnainer">
<% if (message) {%>
<p class="text-container text-danger">
<%= message %>
</p>
<%}%>
<% if (shipper_names) {%>
<form class="add-player-form" action="" method="POST" enctype="multipart/form-data">
<div>
<input type="text" id="shippers_names" list="languageList" />
<!--your input textbox-->
<datalist id="languageList">
<option value=""> </option>
</datalist>
</div>
<button type="submit" class="btn">Add Product</button>
</form>
<% } else { %>
<p class="text center">Product Not Found. Go HereTo Add Product.</p>
<% } %>
</div>
</html>
module.exports = {
addProductPage: (req, res) => {
let query = "SELECT * from shipper_names"
conn.query(query, (err, results) => {
if (err) {
return res.status(500).send(err);
}
res.render('add-product.ejs', {
shipper_names: results
});
});
},
<div>
<input type="text" id="txtAutoComplete" list="names" />
<!--your input textbox-->
<datalist id="names">
<% shipper_names.forEach((shipper_names, index)=>{%>
<option id=<%= shipper_names.Shipper_ID%>>
<%= shipper_names.Shipper_Name%></option>
<%})%>
</datalist>
</div>
this is working

Trying to Eliminate Players in a Tournament via button

I am trying to implement the update route into a poker site I am building. I have been able to get the players to appear on the show page but I would like to update the mongo DB from inTournament: "Yes" to inTournament: "No" when the Eliminate button is selected for that player. I have included my code below. Thank you, in advance, for any help with this.
======================= app.js UPDATE ROUTE=======================
app.get("/seating", function(req, res){
//Get all players from poker_DB
Seat.find({inTournament: "Yes"}, function(err, allseats){
if(err){
console.log(err);
} else {
shuffle(allseats);
res.render("seating", {seats: allseats});
}
});
});
//Update player's tournament status
app.put("/seating", function(req, res){
Seat.findByIdAndUpdate(req.params.id, req.params.inTournament, function(err, updatedStatus){
if(err) {
console.log(err);
} else {
res.send("Data Updated");
}
});
});
======================= SHOW PAGE=======================
<div class ="row text-left" style="display:flex; flex-wrap: wrap;">
<div class="col-md-4 table table-bordered">
<h3 class="text-center">Table 1</h3>
<ol>
<% var i = 1; %>
<% var j = 0; %>
<% seats.forEach(function(seat){ %>
<% if(seat.player && i < perTable1){ %>
<% console.log(perTable2); %>
<li><%= seats[j].player %>
<form action"/seating?_method=PUT" method="POST">
<button class="btn btn-sm btn-warning" type="Submit">Eliminate</button>
</form>
</li>
<% i++ %>
<% j++ %>
<% } else { %>
<% j++ %>
<% } %>
<% return j; %>
<% }); %>
</ol>
</div>
</div>

Elastic search is returning undefined for _id in elastic search

I am using elastic search in my application and everything works fine until you click the search result link which returns undefined. The problem I have is that the _source._id is returning undefined. So I cannot view the search result.
How can I pass the object id in /blog/article/<%= data[i]._source._id?
My code is below
router.js
router.post('/search', function(req, res, next) {
res.redirect('/search?q=' + req.body.q);
});
router.get('/search', function(req, res, next) {
if (req.query.q) {
Article.search({
query_string: { query: req.query.q}
}, function(err, results) {
results:
if (err) return next (err);
var data = results.hits.hits.map(function(hit) {
return hit;
});
res.render('main/search-result', {
query: req.query.q,
data: data
});
});
}
});
search-result.ejs
<% for (var i=0; i < data.length; i++) { %>
<div class="col-md-4">
<a href="/blog/article/<%= data[i]._source._id %>">
<div class="thumbnail">
<h3 id="data_title"><%= data[i]._source.title %></h3>
</div>
</div>
<% } %>
Finally resolved this issue by changing
<a href="/blog/article/<%= data[i]._source._id %>">
to
<a href="/blog/article/<%= data[i]._id %>">

Resources