Mongoose update function is giving null and not actually updating information - javascript

My .findOneAndUpdate method is returning user as null and isn't ending up updating the information. Everything seems to be in order, I'm not getting any erros.
EDIT: I have made progress, I was able to finally update the GroupID, but its setting it as null. Instead of the passed in string.
router.put("/update", (req, res) => {
Users.findOneAndUpdate(
{ _id: req.body.id },
{
$set: { GroupID: req.body.GroupID }
},
{ new: true },
(err, user) => {
if (err) res.send(err);
else res.send("Account GroupID Updated" + user);
}
);
});

You have to convert req.body.id to objectId as follows:
var mongoose = require('mongoose');
var id = mongoose.Types.ObjectId(req.body.id);
Users.findOneAndUpdate(
{ _id: id }

Related

Error in updating profile with image using mongoose and cloudinary

updateProfile: async function(req, res) {
try {
const update = req.body;
const id = req.params.id;
if (!req.files || Object.keys(req.files).length === 0) {
return res.status(400).send('No files were uploaded.');
}
const image = req.files.profileImage;
const cloudFile = await upload(image.tempFilePath);
const profileImage = cloudFile.url
console.log('Loging cloudfile', profileImage)
await User.updateOne(id, { update }, { profileImage }, { new: true },
function(err, doc) {
if (err) {
console.log(err)
}
if (doc) {
return res.status(200).send({ sucess: true, msg: 'Profile updated successful' })
}
});
} catch (error) {
res.status(500).json({ msg: error.message });
}
}
But I'm getting an error of "Callback must be a function, got [object Object]"
I have tried to $set: update and $set: profileImage but still not working.
So the image successful upload into the cloudinary but the update for mongoose is not working.
Upon brief research into the issue, I think you are feeding the arguments in wrong. Objects can be confusing but not to worry.
Your code is:
await User.updateOne(id, { update }, { profileImage }, { new: true }
However, I believe it should be something more like:
await User.updateOne({id: id}, { profileImagine: profileImage, new: true },
The API reference annotates use of the function as:
const filter = { name: 'John Doe' };
const update = { age: 30 };
const oldDocument = await User.updateOne(filter, update);
oldDocument.n; // Number of documents matched
oldDocument.nModified; // Number of documents modified

Mongoose - Model.deleteOne() is deleting the entire collection instead of a single document

I have a User model that contains an array of customers. I want to delete a specific customer based on the customer _id. From what I've read in the Mongoose docs, I should use Model.deleteOne to delete a single document.
Here is my attempt
User Schema (it's been shortened for brevity):
const mongoose = require('mongoose');
const UserSchema = new mongoose.Schema({
username: {
type: String,
default: ''
},
password: {
type: String,
default: '',
},
registerDate: {
type: Date,
default: Date.now()
},
customer: [{
name: {
type: String,
default: '',
},
email: {
type: String,
default: 'No email name found'
},
fleet: [{
unitNumber: {
type: String,
default: 'N/A',
}
}]
}]
});
module.exports = mongoose.model('User', UserSchema);
Here is a look at the route and controller:
const express = require('express');
const router = express.Router();
const customer_controller = require('../../controllers/customers');
router.delete('/customers/:custid', customer_controller.customer_remove);
module.exports = router;
And finally the controller:
exports.customer_remove = (req, res) => {
const { params } = req;
const { custid } = params;
User.deleteOne({ 'customer._id': custid }, (err) => {
if (err)
throw err;
else
console.log(custid, 'is deleted');
});
};
From what I thought, User.deleteOne({ 'customer.id': custid }) would find the customer _id matching the custid that is passed in via the req.params. When I test this route in Postman, it deletes the entire User collection that the customer is found in, instead of just deleting the customer. Can I get a nudge in the right direction? I feel like I am close here (or not lol).
deleteOne operates at the document level, so your code will delete the first User document that contains a customer element with a matching _id.
Instead, you want update the user document(s) to remove a specific element from the customer array field using $pull. To remove the customer from all users:
User.updateMany({}, { $pull: { customer: { _id: custid } } }, (err) => { ...
Using Mongoose you can do this:
model.findOneAndUpdate({ 'customer._id': custid }, {$pull: { $pull: {
customer: { _id: custid } }}, {new: true}).lean();
Removing subdocs.
Each sub document has an _id by default. Mongoose document arrays have a special id method for searching a document array to find a document with a given _id.
Visit: https://mongoosejs.com/docs/subdocs.html
parent.children.id(_id).remove();
Use async-await, may be that will work.
exports.customer_remove = async (req, res) => {
const { params } = req;
const { custid } = params;
try {
await User.deleteOne({ 'customer._id': custid });
console.log(custid, 'is deleted');
} catch (err) {
throw err;
}
};

querying mongodb collection for regex using mongoose

I am trying to query using regex for mongoose, I have seen other posts which have similiar suggestions but I still couldn't figure out, and also getting new errors instead of just getting a null document back.
I am trying to query value contains instead of the need of the exact to get results
for my route, I have something like this
router.get('/:name/:value', (req, res, next) => {
const o = {};
const r = `.*${req.params.value}.*`;
// the above gives me error such as CastError: Cast to string failed for value "{ '$regex': '.*y.*' }" at path "username" for model "Model"
o[req.params.name] = { $regex: { $regex: r }, $options: 'i' };
Model.find(o, (err, doc) => {
if (err) return next(err);
res.send('success');
});
});
can someone give me a hand where I have been doing wrong?
Thanks in advance for any help.
Suppose below is your Model
//Employee.js
import mongoose from 'mongoose';
const Employee = mongoose.Schema({
Name: { type: String, default: "" },
Age: { type: Number, default: 0 },
Email: { type: String, default: "" },
}, { collection: 'Employee' });
export default mongoose.model('Employee', Employee);
Your router must be like below
var Employee = require('../path/to/Employee.js');
router.get('/name/:value', (req, res, next) => {
let query = {
Name: {
$regex: req.params.value,
$options: "i"
}
};
Employee.find(query, (err, docs) => {
if (err) return next(err);
console.log("Documents-->", docs)
res.send('success');
});
});
You no need to give separate param for name just do query like above

collection.save() not persisting data to mongodb

I'm totally new to Nodejs, and I've been trying to update a field in my collection to no avail. Here's my code for pushing into an array on the model.
update array field in model
Then here's my schema.
course schema
Any reason why the changes are not saved to my database ? Thanks .
NB: I've tried using markModified('enrolled_courses.completed_lesson'), but it didn't make any difference.
router.route('/yourRoute').post((req, res) => {
Schema.findOneAndUpdate({
username: newName
}, {
$set: {
enrolled_courses: [
{
key: newValue
}
]
}
}, {
upsert: true
}, function(err, user) {
if (err) {
res.json(err);
} else {
res.send({message: "Successfully updated"});
}
})
});

Cannot read property 'push' of undefined

Here's my mongoose schema:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var CartSchema = new Schema({
userID: String,
items: [{
itemID: String,
quantity: Number
}]
});
module.exports = mongoose.model('Cart', CartSchema);
Here's the node.js server which uses Express. It has /add-to-cart route which if triggered it should update user's cart with the information passed in req.body:
router.post('/add-to-cart', function(req, res, next) {
Cart.find({ userID: req.body.userID }).then(function(userCart){
console.log("TEST: "+JSON.stringify(userCart));
var myItem = {itemID: req.body.itemId, quantity: 1}
userCart.items.push(myItem);
res.send(userCart);
}).catch(next);
});
I printed to terminal userCart as you can see in my code and it returned me this:
[{
"_id":"58f7368b42987d4a46314421", // cart id
"userID":"58f7368a42987d4a46314420", // userid
"__v":0,
"items":[]
}]
When the server executes userCart.items.push(myItem); it returns this error:
Cannot read property 'push' of undefined
Why items is not defined if I've already defined its structure in mongoose?
As adeneo correctly pointed out, userCart is clearly an array but you need to use one of the update methods to push the document to the items array, would suggest Model.findOneAndUpdate() as in
router.post('/add-to-cart', function(req, res, next) {
Cart.findOneAndUpdate(
{ userID: req.body.userID },
{ $push: { items: { itemID: req.body.itemId, quantity: 1 } } },
{ new: true }
)
.exec()
.then(function(userCart) {
console.log("TEST: "+ JSON.stringify(userCart));
res.send(userCart);
})
.catch(next);
});
As adeneo pointed out userCart is an array since you are you are using the find method. But clearly you need to find just one document given by its userID so it advised to use findOne() instead.
Also you will need to save the document in order for the changes to actually reflect.
Have a look at the updated code below:
router.post('/add-to-cart', function(req, res, next) {
Cart.findOne({ userId: req.body.userID }, function(err, userCart){
if(err) return next(err);
console.log("TEST: "+JSON.stringify(userCart));
var myItem = {itemID: req.body.itemId, quantity: 1}
userCart.items.push(myItem);
userCart.save(function(err, usersCart) {
if(err) return next(err);
res.send(usersCart);
})
})
});
Hope this helped.
This can be solved using:
router.post('/add-to-cart', function(req, res, next) {
Cart.findOne({ userID: req.body.userID }).then(function(userCart){
console.log("TEST: "+JSON.stringify(userCart));
const a = req.body.items;
for(i=0;i<a.length;i++)
{
userCart.items.push(req.body.items[i]);
}
res.send(userCart);
}).catch(next);
});

Categories

Resources