I have a query that returns results from my database, but I don't see how I can have it give me results from multiple queries.
router.get("/", function(req, res) {
pg.query("SELECT * from tic", (err, done) => {
if (err) {
console.log(err);
}
res.render("index", { tic: done.rows });
});
});
I was trying along the lines of this, but cant get it to work since he render statement is inside the query and I can get the render to see the results when I move it out of there
router.get("/", function(req, res) {
pg.query("SELECT * from tic", (err, tic) => {
if (err) {
console.log(err);
}
pg.query("SELECT * from tac", (err, tac) => {
if (err) {
console.log(err);
}
});
res.render("index", { tic: tic.rows }, { tac: tac.rows});
});
You just have to render after the second query executes. To do that you have to render in the callback of the second query.
router.get("/", function (req, res) {
pg.query("SELECT * from tic", (err, tic) => {
if (err) {
console.log(err);
}
pg.query("SELECT * from tac", (err, tac) => {
if (err) {
console.log(err);
}
res.render("index", { tic: tic.rows }, { tac: tac.rows });
});
});
});
Related
I am trying to pass a query from MySQL to .ejs file, and send object at the same time.
In the following code, I am trying to send rows and { pageTitle: "Edit Agents" } to edit_agents page
Router.get("/", (req, res) => {
mysqlConnection.query("select * from agent", (err, rows, fields) => {
if (!err) {
res.render("edit_agents", rows);
res.render("edit_agents", { pageTitle: "Edit Agents" });
} else {
console.log("error");
}
});
});
Just to let you know, I am using NodeJs (express framework) with ejs views.
Router.get("/", (req, res) => {
mysqlConnection.query("select * from agent", (err, rows, fields) => {
if (!err) {
res.render("edit_agents", { data: rows,pageTitle: "Edit Agents" });
} else {
console.log("error");
}
});
});
i have this project where im populating a lists collection in database with todos in the show page of a 'board'.
This is the board showpage route
app.get('/boards/:id', (req, res) => {
Board.findById(req.params.id).populate('lists').exec(function(err, foundBoard) {
if (err) {
console.log(err.message);
} else {
List.findById(req.params.id).populate("todos").exec(function (err, foundlist) {
if (err) {
console.log(err);
} else {
console.log(foundlist);
res.render('show', { board: populatedboard ,list:foundlist});
}
});
}
});
});
and this is the route where the variable is i want to access.i want to access and use that variable in the board show route (const listid is the variable i want to use in the board show route)
app.post("/boards/:id/lists/:listid/todos",function(req,res){
Board.findById(req.params.id, function (err, foundBoard) {
if (err) {
console.log(err);
} else {
Todo.create(req.body.todo, function (err, newtodo) {
if (err) {
console.log(err);
} else {
console.log(newtodo);
List.findById(req.params.listid, function (err, foundlist) {
if (err) {
console.log(err)
} else {
**const listid**= req.params.listid;
foundBoard.todos.push(newtodo);
foundBoard.save();
res.redirect("/boards/" + foundBoard._id);
}
});
}
});
}
});
})
I was troubleshooting why my route wasn't working and i came across this.
In my ./routes/jobs.js,
router.delete("/:id", (req, res) => {
Job.findByIdAndDelete(req.params.id, (err, job) => {
if (!err) {
res.json({ msg: "job deleted"});
} else {
console.log(err);
}
});
});
When i tested on postman, Delete - http://localhost:5000/dashboard/60b9405e1ea
Would return the id only 60b9405e1ea and not delete the db job.
I changed my route to "/" and tested it out. using http://localhost:5000/dashboard in postman.
router.delete("/", (req, res) => {
Job.findByIdAndDelete(req.params.id, (err, job) => {
if (!err) {
res.json({ msg: "job deleted"});
} else {
console.log(err);
}
});
It executed the delete request with {msg: "job deleted"}. (Obviously didnt delete db job since no id was given).
Keep in mind in my server.js im using,
app.use("/dashboard", require("./routes/jobs"));
Any help would be appreciated on why /:id is not being executed
As you are getting the id in the console, it's the problem with the query you make.
Try any of these,
Model.remove({ _id: req.body.id }, function(err) {
if (!err) {
message.type = 'notification!';
}
else {
message.type = 'error';
}
});
or
Model.findOneAndRemove({id: req.params.id}, function(err){
});
or a traditional approach:
Model.findById(id, function (err, doc) {
if (err) {
// handle error
}
doc.remove(callback); //Removes the document
})
I'm really new to node/express/mongoDB coding, and I have a slight problem with adding/updating values into mongoDB via node/express.
app.post('/', (req, res, next) => {
let data = {
first_value: req.body.first_value,
second_value: req.body.second_value,
};
dbase.collection("testDB").insertOne(data, (err, result) => {
if (err) {
console.log(err);
}
res.send('data added successfully');
});
});
app.put('/:id', (req, res, next) => {
var id = { _id: new ObjectID(req.params.id) };
dbase.collection("testDB").updateOne({ _id: id }, {
$set: {
first_value: req.body.first_value
second_value: req.body.second_value,
}
}, (err, result) => {
if (err) {
throw err;
}
res.send('data updated sucessfully');
});
});
app.put does not alter the values in DB, and app.post only adds "null" into every section of the new entry when I'm trying them with Postman. When I add new values with html form, the data is added correctly.
What is the problem with my code?
For app.post , can you provide me a screen shot of the way you are entering data and in which format e.g. , application/raw , application/x-www-form-urlencoded etc .
For app.put you need to correct the following things . The corrected code is as below ,
app.put('/:id', (req, res, next) => {
var id = { _id: new ObjectID(req.params.id) };
dbase.collection("testDB").updateOne( id , { // put "id" instead of "{ _id: id }"
$set: {
first_value: req.body.first_value
second_value: req.body.second_value,
}
}, (err, result) => {
if (err) {
throw err;
}
res.send('data updated sucessfully');
});
});
Hope you can get the point and this works for you .
I have a router like that:
app.get('/rest/userList', (req, res) => {
UserList.find({}, (err, users) => {
if (err) res.send(err);
res.json(users);
});
});
I would like to change it like this:
app.get('/rest/userList', getUsers);
function getUsers(req, res) {
UserList.find({}, createResponse);
}
function createResponse(err, users) {
if (err) return res.send(err);
return res.send(users);
}
However; in createResponse function 'res' is undefined. How can I do that?
You can use .bind() to bind extra parameters to the callback:
app.get('/rest/userList', getUsers);
function getUsers(req, res) {
UserList.find({}, createResponse.bind(null, res));
}
function createResponse(res, err, users) {
if (err) return res.send(err);
return res.send(users);
}
app.get('/rest/userList', getUsers, createResponse);
function getUsers(req, res, next) {
UserList.find({}, function (err, users) {
if (err) {
req.errr = err;
} else {
req.users = users;
}
next();
});
}
function createResponse(req, res) {
if (req.err) return res.send(req.err);
return res.send(req.users);
}