Need Help.Node.Js With Ajax and MongoDB CRUD Working slowly Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callAbort Ajax requests using jQueryjQuery AJAX submit formMongoDB vs. CassandraHow to query MongoDB with “like”?How to make an AJAX call without jQuery?How do I drop a MongoDB database from the command line?Is Safari on iOS 6 caching $.ajax results?how do you connect angular js to node js
Dating a Former Employee
Why is it faster to reheat something than it is to cook it?
Performance gap between vector<bool> and array
What is the topology associated with the algebras for the ultrafilter monad?
Project Euler #1 in C++
Why do we bend a book to keep it straight?
Is a ledger board required if the side of my house is wood?
Effects on objects due to a brief relocation of massive amounts of mass
What initially awakened the Balrog?
Why does the remaining Rebel fleet at the end of Rogue One seem dramatically larger than the one in A New Hope?
Using et al. for a last / senior author rather than for a first author
Amount of permutations on an NxNxN Rubik's Cube
Disembodied hand growing fangs
What's the meaning of "fortified infraction restraint"?
Is there any word for a place full of confusion?
Sum letters are not two different
Why weren't discrete x86 CPUs ever used in game hardware?
How often does castling occur in grandmaster games?
Illegal assignment from sObject to Id
Can anything be seen from the center of the Boötes void? How dark would it be?
Question about debouncing - delay of state change
An adverb for when you're not exaggerating
What is this clumpy 20-30cm high yellow-flowered plant?
Find 108 by using 3,4,6
Need Help.Node.Js With Ajax and MongoDB CRUD Working slowly
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
The Ask Question Wizard is Live!How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?How to manage a redirect request after a jQuery Ajax callAbort Ajax requests using jQueryjQuery AJAX submit formMongoDB vs. CassandraHow to query MongoDB with “like”?How to make an AJAX call without jQuery?How do I drop a MongoDB database from the command line?Is Safari on iOS 6 caching $.ajax results?how do you connect angular js to node js
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
When I try to add or delete from my database it takes a few seconds for the page to update. I use AJAX and it is still very slow. I can not figure out what the problem is. I am new at NODE JS I came from MVC NET. Thanks and here is my code.
app.js
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
mongodb.js
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
And This Is My View-mongo.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
thanks this is greate place!!
node.js ajax mongodb express ejs
add a comment |
When I try to add or delete from my database it takes a few seconds for the page to update. I use AJAX and it is still very slow. I can not figure out what the problem is. I am new at NODE JS I came from MVC NET. Thanks and here is my code.
app.js
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
mongodb.js
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
And This Is My View-mongo.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
thanks this is greate place!!
node.js ajax mongodb express ejs
add a comment |
When I try to add or delete from my database it takes a few seconds for the page to update. I use AJAX and it is still very slow. I can not figure out what the problem is. I am new at NODE JS I came from MVC NET. Thanks and here is my code.
app.js
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
mongodb.js
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
And This Is My View-mongo.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
thanks this is greate place!!
node.js ajax mongodb express ejs
When I try to add or delete from my database it takes a few seconds for the page to update. I use AJAX and it is still very slow. I can not figure out what the problem is. I am new at NODE JS I came from MVC NET. Thanks and here is my code.
app.js
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
mongodb.js
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
And This Is My View-mongo.ejs
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
thanks this is greate place!!
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
let express = require('express');
let app = express();
let mongodb=require('./mongodb')
let bodyParser = require('body-parser');
let index=require('./index');
app.use(bodyParser.urlencoded(
extended: false
));
app.use(bodyParser.json());
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
app.listen(8080, function ()
console.log('Example app listening on port 8080!');
);
app.use('/',index);
app.use('/mongo',mongodb);
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
let express = require('express');
let MongoClient = require('mongodb').MongoClient;
let url = "mongodb://localhost:27017/";
let router = express.Router();
let obj = null;
router.get('/', function (req, res)
getDfroMongo(res);
);
function getDfroMongo(res)
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
//Sort the result by name:
var sort = car: 1 ;
dbo.collection("customers").find().sort(sort).toArray(function (err, result)
if (err) throw err;
obj = result;
// db.close();
res.render(__dirname + '/views/mongo', result: obj );
);
);
router.post('/', function (req, response)
switch (req.body.data)
case "add":
let car = req.body.car;
let model = req.body.model;
let year = req.body.year;
let myimg = req.body.img;
obj = car: car, model: model, year: year, img: myimg ;
if (car != "" && model != "" && year != "" && myimg != "")
console.log(obj);
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myobj = Car: car, Model: model, Year: year, img: myimg ;
dbo.collection("customers").insertOne(myobj, function (err, res)
if (err) throw err;
console.log("1 document inserted");
// db.close();
getDfroMongo(response);
);
);
else
console.log('field missed');
break;
case "delete":
let carName = req.body.carName;
MongoClient.connect(url, function (err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: carName ;
dbo.collection("customers").deleteOne(myquery, function (err, obj)
if (err) throw err;
console.log("1 document deleted");
// db.close();
getDfroMongo(response);
);
);
break;
case "update":
upcar = req.body.car;
upmodel = req.body.model;
upyear = req.body.year;
upimg = req.body.img;
upcarName = req.body.carName;
// obj = car: upcar, model: upmodel, year: upyear, img: upimg, carName: upcarName ;
MongoClient.connect(url, function(err, db)
if (err) throw err;
var dbo = db.db("mydb");
var myquery = Car: upcarName ;
var newvalues = $set: Car: upcar, Model: upmodel,Year:upyear,img:upimg ;
dbo.collection("customers").updateOne(myquery, newvalues, function(err, res)
if (err) throw err;
console.log("1 document updated");
// db.close();
getDfroMongo(response);
);
);
break;
default:
break;
);
module.exports=router;
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="/css/style.css" rel="stylesheet" type="text/css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<%include title%>
</head>
<body>
<button type="button" name="addDB" id="addDB" onclick="AddDbFunction()">insert</button>
<input type="text" name="car" id="car" placeholder="car">
<input type="text" name="model" id="model" placeholder="model">
<input type="text" name="year" id="year" placeholder="year">
<input type="text" name="img" id="img" placeholder="img"><br>
<button type="button" name="update" id="update" onclick="updateFunction()">update</button>
<button type="button" name="delete" id="delete" id="delete" onclick="deleteFunction()">delete</button>
<input type="text" name="carName" id="carName" placeholder="carName">
<script>
function updateFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
let carName = document.getElementById("carName").value;
let parameters= data: "update", car: car, model: model, year: year, img: img, carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function deleteFunction()
let carName = document.getElementById("carName").value;
// $.post('/', data: "delete", carName: carName );
let parameters = data: "delete", carName: carName ;
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
function AddDbFunction()
let car = document.getElementById("car").value;
let model = document.getElementById("model").value;
let year = document.getElementById("year").value;
let img = document.getElementById("img").value;
// $.post('/', data: "add", car: car, model: model, year: year, img: img );
let parameters = data: "add", car: car, model: model, year: year, img: img
$.ajax(
url: '/mongo',
type: 'POST',
data: parameters,
success: function ()
location.reload();
);
</script>
<table>
<tr>
<th>Car</th>
<th>Model</th>
<th>Year</th>
<th>image</th>
<!-- <th>id</th> -->
</tr><br>
<% for(let i=0; i < result.length; i++) %>
<tr>
<td><%= result[i].Car%></td>
<td><%= result[i].Model%></td>
<td><%= result[i].Year%></td>
<!-- <td><%= result[i]._id%></td> -->
<td> <img src="images<%= result[i].img%> " width="80" height="80"></td><br>
</tr>
<% %>
</table>
</body>
</html>
node.js ajax mongodb express ejs
node.js ajax mongodb express ejs
asked Mar 8 at 20:19
Baruch MashashaBaruch Mashasha
33
33
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Don't connect mongo for each request, just connect once at the start of your script.
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070458%2fneed-help-node-js-with-ajax-and-mongodb-crud-working-slowly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't connect mongo for each request, just connect once at the start of your script.
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
add a comment |
Don't connect mongo for each request, just connect once at the start of your script.
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
add a comment |
Don't connect mongo for each request, just connect once at the start of your script.
Don't connect mongo for each request, just connect once at the start of your script.
answered Mar 8 at 20:43
MattMatt
1807
1807
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
add a comment |
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Thanks man. Work Perfect now.
– Baruch Mashasha
Mar 8 at 21:19
Your'e welcome :)
– Matt
Mar 8 at 21:40
Your'e welcome :)
– Matt
Mar 8 at 21:40
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55070458%2fneed-help-node-js-with-ajax-and-mongodb-crud-working-slowly%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown