Node.js - print result from sql - javascript

I am trying to run a select query from db and print results. But even i see result in console i don't see in index page. (Hovewer i see result in console but it is also doesn't show correctly. I have 2 rows in db but i see 3 lines for each row. So result in console is : 2X3=6 rows.) I put screenshot about console result end of the question.
Code in app.js
app.use('/', routes, function(req, res){
pg.connect(connect, function(err, client, done){
if(err){
return console.error('errrr', err)
}
client.query('select * from recipes', function(err, result){
if(err){
return console.error('error running query', err);
}
console.log(result.rows);
res.render('index.njk', { recipes: result.rows});
done();
});
});
});
Code in index.njk
<ul>
{% for name, item in recipes %}
<li>{{ name }}: {{ item.name }}</li>
{% endfor %}
</ul>
this is result of the console
Can you please help me to fix it?

It seems like (via your picture of the console) that result.rows is an array of arrays that has 2 values in it. Therefore, while looping over the value in your index.njk, recipes is an array of arrays and doesn't contain an item that has a name attribute.
If you set recipes: result.rows[0] that should provide a quick fix:
res.render('index.njk', { recipes: result.rows[0]});
This allows you to get the first element in your array of arrays, which is okay, because the array held within only has 1 element (the 2 items you actually want!). You should check before you do this that result.rows.length > 0 so you don't go out of bounds and get an error.
if(result.rows.length > 0) {
res.render('index.njk', { recipes: result.rows[0]});
}else {
console.log('No rows found in DB');
}

I have solved my issue by using below code block in index.js instead of using app.js. I am not sure it is correct way but it is working fine now. If it is not correct way, let me correct it please.
router.get('/', function(req, res){
pg.connect(connect, function(err, client, done){
if(err){
return console.error('errrr', err)
}
client.query('select * from recipes', function(err, result){
if(err){
return console.error('error running query', err);
}
if(result.rows.length > 0) {
res.render('index.njk', { recordResult: result.rows});
console.log(result.rows);
}else {
console.log('No rows found in DB');
}
done()
});
});
});

Related

.toArray not workin

i have more than 20 products on the database and i am trying to display all of them so i used
Product.find({}).toArray(function(err,data){
if(err){
res.send(err)
}
if(data){
res.send(data)
}
}
but i get an error
TypeError: Product.find(...).toArray is not a function
so then i used
Product.find({},function(err,products){
if(err){
res.send(err)
}
if(products){
res.send(products)
}
})
but it only prints out 20 products. so then i tried
Product.find({},function(err,products){
if(err){
res.send(err)
}
if(products){
res.send(products)
}
}).limit(300)
but it still prints out 20 products
Use promises instead of using callbacks
Try this:
Products.find().then(products => {
res.send({ products })
}).catch(err => {
res.send({ err })
})
It should retrieve all products instead of just 20
If it retrieves just 20, check how much do you have using the .count() method
You should add options like limit before the toArray-call. Additionally I assume you have a default limit set by your included mongodb library.
This code sample should give you 300 products:
Product
.find({})
.limit(300)
.toArray(function(err,data) {
if (err) {
res.send(err);
} else if (data) {
res.send(data);
}
)};
For reference see mongodb-native #find and/or mongodb-native Cursor

in nodejs how to insert the record into PostgreSQL if it does not exist?

router.post('/university', function (req, res, next) {
pg.connect(connectionString,function(err,client,done) {
if(err){
console.log("not able to get connection "+ err);
res.status(400).send(err);
}
client.query("select university_name from university where university_name = '"+req.body.university_name+"'",function(err,data)
{
if(data) {
res.send({message:"exist"});
}
else
{
client.query("INSERT INTO university (_id, university_name, status) VALUES (nextval('university_id_seq'), '"+req.body.university_name+"', '"+req.body.status+"')", function(err, result) {
done();
if(err){
console.log(err);
res.status(400).send(err);
}
res.send({message : "successfully inserted"});
});
}
});
it displays university_name as exist even it is not present on every entry,
how to insert the record into PostgreSQL if does not exists?
It depends on which DB driver you are using, anyway most common is that when SELECT query is executed, you will get result data even if records in the DB doesn't exists. Usually result object then has property rows which you should check.
In your case similar to this:
if(data.rows && data.rows.length > 0){
// there is data in the DB
} else {
// no data in the DB
}

MongoDB - Mongoose: How Do I Update Simultaneously Two Separate Embedded Documents With Mongoose?

So I am trying to update the field status in my Report document and in my Station.reports sub-document which is an array of objects, in one single API call. The issue is that I am able to update the Report document, but not the station document when making the API call. After the call, the console.log(station.reports); returns the expected subdocument which is : [{"_id":"588fed278b50cd180bd6cc15","date":"2017-01-31T01:48:57.487Z","status":"Archived"}] But this is not saved in the corresponding Station document in my DB. Please I need help here. Thanks.
Station Document:
{
"_id": "588a777d4e26720e7afa7e1e",
"phone": "(007) – 007 – 7007",
"name": "name1",
"email": "name1#email.com",
"reports": [
{
"status": "Submitted",
"date": "2014-01-31T01:48:57.487Z",
"_id": "588fed278b50cd180bd6cc15"
}
]
}
Report Document
{
"_id": "588fed278b50cd180bd6cc15",
"description": "Description of the report",
"time": "05:48 PM",
"date": "2017-01-31T01:48:57.487Z",
"status": "Archived",
"location" : "123 Main Street"
"station" : "588a777d4e26720e7afa7e1e"
}
API Call
router.put('/reports/:id/updateStatus', function (req, res) {
Report.findById(req.params.id, function(err,report){
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
return res.send(err);
// Update the Report object
report.status = req.body.status;
// Update the Corresponding station.reports subdocument
Station.findOne({_id:report.station}, function (err, data) {
if(err) return console.log(err);
data.reports.forEach(function(rpt){
if (rpt._id == req.params.id){
rpt.status = req.body.status
data.save(function (err, station) {
if (err)
return res.send(err);
console.log(station.reports);
})
}
})
})
report.save(function (err, report) {
if (err)
return res.send(err);
res.json(report);
})
});
})
You are doing mistake while updating the station object. Use findOneAndUpdate to find the matching Station document, and then change the status of the matched reports item(matched using reports._id).
Try this:
Station.findOneAndUpdate({
_id:report.station,"reports._id":req.params.id
},{
$set : {reports.$.status : req.body.status}
},function(err){
if(err)
return res.send(err);
});
report._id will find the array element whose _id is req.params.id and report.$.status will update only the matching element of the array.
For more information on positional $(update) operator, Read mongoDB positional Documentation.
Also, i would suggest to save the report object in the callback of update. As nodejs is asynchronous, it will not wait for the update to finish, if you are saving report outside of the callback. And, you might get Cant set the headers after they are sent error. Thus, its recommended to do it in the callback.
Thus your final API code would look like:
router.put('/reports/:id/updateStatus', function (req, res) {
Report.findById(req.params.id, function(err,report){
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
return res.send(err);
// Update the Report object
report.status = req.body.status;
// Update the Corresponding station.reports subdocument
Station.findOneAndUpdate({
"_id":report.station,"reports._id":req.params.id
},{
$set : {"reports.$.status" : req.body.status}
},function(err, result){
if(err)
return res.send(err);
console.log(result);
report.save(function (err, report) {
if (err)
return res.send(err);
res.json(report);
});
});
});
})
UPDATE
Alternate Method
Another way can be, You can proceed in the original way, but don't save the data inside the forEach, instead save the data sheet forEach finishes.
Station.findOne({_id:report.station}, function (err, data) {
if(err) return console.log(err);
data.reports.forEach(function(rpt){
if (rpt._id == req.params.id){
rpt.status = req.body.status
}
});
data.save(function (err, station) {
if (err)
return res.send(err);
console.log(station.reports);
report.save(function (err, report) {
if (err)
return res.send(err);
res.json(report);
});
})
})
Hope this helps!
After multiple attempts, and with the help of Ravi, I was able to figure out a solution that worked for me pretty well. The only thing that changed was my API call. The rest of the code was unchanged.
Hope this helps someone having similar needs.
API CALL
router.put('/reports/:id/updateStatus', function (req, res) {
Report.findById(req.params.id, function(err,report){
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
return res.send(err);
// Update the Report object
report.status = req.body.status;
// Update the Corresponding station.reports subdocument
Station.findOne({_id:report.station}, function (err, info) {
if(err) return console.log(err);
info.reports.forEach(function(rpt){
if (rpt._id == req.params.id){
Station.update({_id:info._id, "reports._id":rpt._id },
{
$set:{"reports.$.status": req.body.status }
},function (err, results) {
if(err) return console.log("This Station couldn't be updated " + err);
console.log(results)
}
)
}
})
report.save(function (err, report) {
if (err)
return res.send(err);
res.json({report:report, station:info});
});
})
});
})

Deleting / removing with Mongoose with multiple parameters

This is my first attempt at deleting data in a MongoDB database. I'm loosely following this tutorial (just the delete part) to no avail, https://www.airpair.com/javascript/complete-expressjs-nodejs-mongodb-crud-skeleton. I just want to delete all the requested people who are in the requested country. All of my other requests work so I will just post the code that I know is not working, everything else is fine.
EDIT
The error I get in the log is "404 Not Found". When testing w/ Postman the response I get is, "Cannot DELETE /deletepeople/USA/John"
app.delete('deletepeople/:country/:name', function(req, res) {
var countryReq = req.params.country;
var nameReq = req.params.name;
peopleModel
.find({"country":countryReq}, function(err, country) {
country.find({"name": nameReq}, function (err, person) {
person.remove(function (err, person) {
if (err) {
console.log(err);
res.status(500).send();
}
return res.status(200).send();
})
})
})
});
});
country.find({"name": nameReq}, function (err, person) {
The above line is causing you an error, what are you searching in a returned document? Its just an document and not a collection.
You can use the id() method in embedded docs:
Look at the subdocuments [http://mongoosejs.com/docs/subdocs.html]

Undefined error in MEAN CRUD

{ text: undefined,
done: false,
_id: 529e16025f5222dc36000002,
__v: 0 }
PUT /api/todos/529e16025f5222dc36000002 200 142ms - 68b
I keep getting this error when trying to do an update for my simple CRUD todo list. When I submit the update, the change doesn't appear on screen, although the put says it's a 200. Not sure what steps to take so that I don't get this "undefined" error and so I can have the update show up on screen.
EDIT: Included more code
This is the back-end node code:
app.put('/api/todos/:_id', function(req, res) {
Todo.findById(req.params._id, function(err, todos){
todos.text = req.body.text;
console.log(todos);
todos.save(function() {
if (!err) {
res.send(todos);
} else if (err) {
res.send(err);
}
Todo.find(function(err, todos) {
if (err)
res.send(err);
res.json(todos);
});
});
});
});
This is the Angular front-end code:
$scope.updateTodo = function(id) {
$scope.newItem = prompt("Please enter your new item:", "");
$http.put('/api/todos/' + id, {formData: $scope.newItem}).success(function(data) {
$scope.todos = data;
});
$http.get('/api/todos').success(function(data) {
$scope.todos = data;
});
};
I think it's because of this:
$http.put('/api/todos/' + id, { formData: $scope.newItem} )
^^^^^^^^
You're passing a single formData parameter with the request, yet in your Express code, you use this:
req.body.text
Either try this:
req.body.formData.text
Or don't use the formData parameter at all and pass $scope.newItem directly.
Besides that, your Express code is a bit messy: it might send back multiple responses and it doesn't check for errors on the save (as #PaulGray also pointed out).

Categories