deleteMany showing 0 deleted but actually deleting the documents - javascript

this controller is deleting all the todos of particular user but in response it is showing {n: 0, deletedCount: 0, ok: 1}, does anyone knows the solution of it?
exports.deleteAll = async (req, res) => {
const { id } = req.user
console.log('id', id);
try {
// const deleteAllTodos = await TodoModel.findByIdAndDelete(id)
const deleteAllTodos = await TodoModel.deleteMany({ createdBy: id}, function (err) {
console.log('err', err) })
res.send({ success: true, message: "All todos deleted successfully", deleteAllTodos })
} catch (error) {
console.log('Error:', error.message);
res.status(500).json({
message: "Internal server error",
success: false,
error: error.message
});
}
}

From mongoose Query guides
Don't mix using callbacks and promises with queries, or you may end up
with duplicate operations. That's because passing a callback to a
query function immediately executes the query, and calling then()
executes the query again.
So what you did there was calling deleteMany twice, first using the callback and then using the async/await. The second attempt will try to delete nothing because the first attempt has already deleted them.
You could try using either one:
...
const deleteAllTodos = await TodoModel.deleteMany({ createdBy: id }).exec();
res.send({
success: true,
message: "All todos deleted successfully",
deleteAllTodos
})
Don't worry about error handling using callback as you have already done it with the try/catch block which will catch if any error happened on deleteMany.

Related

where to should write res.send() in node.js app

In my app I have a category collection that saves the category title with its image ID. The image is saved in another collection with its meta data like path , type and so on. So for retrieving category I should retrieve category image in image collection by its ID and add image path to category object that is retrieved from category collection and send it to client...But I don't know where should I send categories to client?
When I send the response face this error :
throw er; // Unhandled 'error' event
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at ServerResponse.header (H:\node.js\online-store\app\node_modules\express\lib\response.js:771:10)
at ServerResponse.send (H:\node.js\online-store\app\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (H:\node.js\online-store\app\node_modules\express\lib\response.js:267:15)
at ServerResponse.send (H:\node.js\online-store\app\node_modules\express\lib\response.js:158:21)
at H:\node.js\online-store\app\controllers\pcategory.controller.js:123:19
at H:\node.js\online-store\app\node_modules\mongoose\lib\model.js:4845:18
at processTicksAndRejections (internal/process/task_queues.js:77:11)
Emitted 'error' event on Function instance at:
at H:\node.js\online-store\app\node_modules\mongoose\lib\model.js:4847:15
at processTicksAndRejections (internal/process/task_queues.js:77:11) {
code: 'ERR_HTTP_HEADERS_SENT'
}
This is my code :
exports.getAll = async (req, res) => {
try{
const categories = await ProductCategory.find({});
categories.map(async(category)=>{
await File.findById(category.imageID).exec(function(err,file){
if(err){
console.log(err)
}else if(file) {
category.imagePath = file.file_path;
tempCategories.push(category)
}
res.send(tempCategories);
})
})
return res.send(tempCategories);
}catch {
res.json(err =>{
console.log(err);
res.status(500).send({
message:
err.message || "There is an error in retrieving category"
});
})
}
}
The problem is that nothing in your code is waiting for the asynchronous operations you're doing in your map callback to complete, so it does the res.send at the end right away — and then does res.send again within the map callback later when the async operations complete. Instead, wait for them to finish and send the result.
Also, you're using res.send where I suspect you want res.json, and using res.json later incorrectly (it doesn't take a callback).
See comments:
exports.getAll = async (req, res) => {
try {
// Get the categories
const categories = await ProductCategory.find({});
// Get the files for the categories, wait for the result
const result = await Promise.all(categories.map(async (category) => {
const file = await File.findById(category.imageID).exec();
// You probably can't modify the `category` object, so let's create
// and return a new object
return {...category, imagePath: file.file_path};
}));
// Send the result converted to JSON
return res.json(tempCategories);
} catch (err) { // Accept the error
// Send an error response
res.status(500).json({
message: err.message || "There is an error in retrieving category"
});
}
};
Side note: Your original code was using map without using the array it creates. That's an antipattern (sadly it seems to be one someone somewhere is teaching). I wrote up why and what to do instead here. (In my update to your code, I still use map, but I use the array it creates, passing it to Promise.all so we can wait for all those promises to settle.)
Your Code Like this,
Now Issue is You are sending two times Headers.
You can use like this, Firstly Declare array and push into it what you need and then last of your logic return it or send it.
exports.getAll = async (req, res) => {
try {
const categories = await ProductCategory.find({});
let tempCategories = []; // New Line
await Promise.all(categories.map(async (category) => {
await File.findById(category.imageID).exec(function (err, file) {
if (err) {
console.log(err)
} else if (file) {
category.imagePath = file.file_path;
tempCategories.push(category)
}
});
return category;
}));
res.send(tempCategories);
} catch {
res.json(err => {
console.log(err);
res.status(500).send({
message:
err.message || "There is an error in retrieving category"
});
})
}
}

MongoDB query with promise not executing if statement (MongoDB node.js driver)

I'm trying to get my head around why the if(!document) is not executed when a document is not found, but instead the app jumps straight to the catch block and returns the 500 status instead?
To clarify, there's no problem with the id sent via req.body.id, if I send a valid ID that matches a doc in the db the success block is executed. The problem is when I send an ID that doesn't match a doc the if (!document) {} block is not executed, but instead the catch block is executed.
My intention is this (if the code example is not clear enough):
if the document is not found return:
return res.status(401).json({ message: 'Unauthorized.' }
If the document is found:
return res.status(200).json({ message: 'Authorized.' });
If there's a db / server error:
return res.status(500).json({ message: 'Internal server error.' })
Small code example below:
const login = (req, res) => {
userSchema
.findById(req.body.id)
.then((document) => {
if (!document) {
return res.status(401).json({ message: 'Unauthorized.' }).end();
}
return res.status(200).json({ message: 'Authorized.' });
})
.catch((err => return res.status(500).json({ message: 'Internal server error.' });
};
In order to successfully execute this part:
userSchema
.findById(req.body.id)
...
req.body.id has to be a value castable to an ObjectId. Otherwise you will see this error CastError: Cast to ObjectId failed for value..., which, of course, will trigger the catch block.
So, for example, if req.body.id is 604156ee6db28527531bd612, which is perfectly castable to ObjectId, your query will actually execute and, if no results are found, will trigger your if block.
If, however, req.body.id is, say, sh21343dfs or an empty string, then findById will throw the CastError mentioned above and send you straing into the catch block.
So just use the catch as your cue to send back something like a User Not Found error, because there are very few other things can trigger that catch block in findById
Or you could expicitly check for the validity of the string like so:
const ObjectID = require('mongodb').ObjectID;
...
...
if(ObjectID.isValid(req.body.id))
return res.status(401).json({ message: 'Unauthorized.' }).end();

Not getting a response using Mongoose

I am creating a document in my MongoDB database using Mongoose with the following code:
workoutsRouter.post('/', async (req, res) => {
await mongoose.connect('mongodb+srv://nalanart:<password>#cluster0.2iplh.mongodb.net/workout-app-db?retryWrites=true&w=majority',
{ useNewUrlParser: true, useUnifiedTopology: true })
await Workout.create({
mains: req.body.mains,
accessories: req.body.accessories
}, err => {
if(err) {
throw err
} else {
res.sendStatus(201)
}
})
})
My problem is that it does not send a 201 status as I have written it to. Any idea why? It just says Sending... on Postman
And here it appears in my collection:
Yeah, because you are both awaiting and passing a callback. You are mixing the await and callback syntaxes. If you pass Mongoose a callback, it won't return a Promise, so it will be awaited forever, since it will never resolve. Either await it, or pass it a callback, not both. Also, try res.status(201).end()
try {
await Workout.create({
mains: req.body.mains,
accessories: req.body.accessories
});
res.status(201).end();
} catch (err) {
throw err
}

get data with find in mongoose and node

I have a problem returning some data from a db in mongodb. I put you in situation.
I have a file called db.js, which has the following content:
const mongoose = require('mongoose');
var libro = mongoose.Schema({
titulo: String,
estado: String,
autor: String,
genero: String
});
module.exports = mongoose.model('estanteria', libro);
I have another file called estanteria.js that has the following content:
const Libreria = require('./db');
const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/libreria', (err) => {
if(err) throw err;
console.log("Conexión a db correcta");
});
...
function allBooks(){
var libros = Libreria.find({})
return libros;
}
exports.allBooks = allBooks;
The problem I have in the function allBooks(), I do not know how to return the contents of the collection in an array of objects, and then display it on the web. Can somebody help me?
Inside allBooks function add a callback function to return after find operation.
function allBooks(){
Libreria.find({}).exec(function(error, records) {
if (!error) {
res.send({
success : true,
records : records
});
} else {
console.log(error);
res.send({
success : false,
error : error
});
}
});
}
exports.allBooks = allBooks;
Use JSON.stringify() to encode libros in a jhson format then write it as an response to the request ( The request recieved by the server )
Libreria.find({}) is an async operation, you need to use Promises way to handle this. As shown below:
Libreria.find returns a promise and you can handle the resolve state of this promise in .then method and if any error occurs it will be done in .catch
function allBooks(){
Libreria.find({})
.then(function(books) {
return books;
})
.catch(function(error){
return error;
})
}
// An exmaple router handler as follow:
router.get("/posts", function(req, res){
allBooks()
.then(function(records){
res.json({status: true, records})
})
.catch(function(error){
res.json({status: false, error })
});
})
Read more about mongoose promises: http://mongoosejs.com/docs/promises.html
Promises in general: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

Sails Waterline ORM query inside array.map()

Code snippet below is using sails waterline ORM to make DB queries and sending response. However, the execution flow is weird, The code outside of map function is running before the map function has finished executing. 'I am outside of map' is being printed in the console before 'I am inside of map'. I think it can be solved this using Promise or async / await. I have tried using Promise.all() below, but it doesn't work, the response is always an empty array. I would be grateful if you could give an example on how to solve this kind of issues.
allMembers: (req, res) => {
const projectId = req.params.id;
ProjectMembers.find({projectId: projectId}).exec( (err, members) => {
if(err) res.serverError("bad request!");
if(members.length === 0) res.notFound({message: "No members are found for this project!"});
let membersInfo = [];
let promise = Promise.all(members.map(m => {
User.findOne({id: m.userId}).exec( (err, user) => {
if(err) membersInfo.push({name: null, userId: null, email:null,rate:null, error: 'Internal error!'})
else if(!user) membersInfo.push({name: null, userId: null, email:null,rate:null, error: 'No user found'})
else membersInfo.push({name: user.name, userId: user.id, rate: m.rate, error: null})
console.log("i am inside of map");
})
}));
console.log("I am outsie of map")
promise.then( (resolve) => {return res.ok({members: membersInfo})});
}
I was about to tell you "don't use queries in .map", but on looking, I think your code is quite close to working. The argument of Promise.all has to be an array of promises. Each User.findOne is indeed a promise - the stumbling block is that once you use .exec it no longer returns a promise.
I think the answer is to do your processing inside the .then instead of right inside the .map:
ProjectMembers.find({projectId: projectId}).exec( (err, members) => {
if(err) return res.serverError("bad request!");
if(members.length === 0) return res.notFound({message: "No members are found for this project!"});
let promise = Promise.all(members.map(m => User.findOne({id: m.userId})));
promise.then( (values) => {
// values is the array of user objects returned from the array of queries
let membersInfo = values.map((user) => {
if (!user) {
return {name: null, userId: null, email:null,rate:null, error: 'No user found'};
} else {
return {name: user.name, userId: user.id, rate: m.rate, error: null};
}
});
return res.ok({members: membersInfo});
}, (err) => {
return res.serverError("Error finding users");
});
The promise only has a single fail callback, so you lose the ability to individually catch and handle querying errors (though you can still individually handle not-found results).

Categories