Matt West
Matt West

Reputation: 1396

Why isn't this Mongoose update saving to the database?

What I have is a function that adds a new date to an array of 'classes held' dates.

Originally, I had a function I'd copied from an example online that was not asynchronous. It used a simple Mongoose 'document.save' call, and worked fine for a few days. Then it stopped updating the array.

So I did some debugging and thought I might need an async function, but that isn't working either. I know I'm doing something wrong, but I don't know what it is.

Here's my code, complete with console.logs to show what the function is doing:

this.updateDate = async function () {
    try {
        let today = this.getFullDate(); // returns a string that I use as a date in the database
        await Admin.findOne({'local.className':'2018'}).then(function(course){
            console.log('today: '+today);
            console.log('course: '+course);
            let classesHeld = course.local.classesHeld;
            console.log('classesHeld: '+classesHeld);
            console.log('index: '+classesHeld.indexOf(today));
            if(classesHeld.indexOf(today)=== -1){
                classesHeld.push(today);
                console.log('added today: '+classesHeld);
                Admin.update({'local.className':'2018'},{'local.classesHeld':classesHeld});
                console.log('saved it');
            }
           else{
                console.log('Today\'s date already in database.');
            }
        });

    } catch (error) {
        console.log('Error in update date: ' + error);
    }
}

Here is what I see in the console:

today: 7/26/2018
course: { _id: 5b57904b0bed9d3004979604,
  __v: 0,
local:
{ className: '2018',
 classesHeld:
  [ '7/16/2018',
    '7/17/2018',
    '7/18/2018',
    '7/19/2018',
    '7/23/2018',
    '7/24/2018',
    '7/25/2018' ] } }
 classesHeld: 
 7/16/2018,7/17/2018,7/18/2018,7/19/2018,7/23/2018,7/24/2018,7/25/2018
 index: -1
 added today: 
 7/16/2018,7/17/2018,7/18/2018,7/19/2018,7/23/2018,
 7/24/2018,7/25/2018,7/26/2018 // this is correct
 saved it // nope

Any help appreciated.

Upvotes: 2

Views: 58

Answers (4)

Matt West
Matt West

Reputation: 1396

Okay, aided by @Pointy's tip that the Mongoose functions I was calling were already asynchronous, and using @cheekujha's suggestion of $push, I got the following code to work:

 this.updateDate = async function () {
    try {
        let today = this.getFullDate();
        Admin.findOne({
            'local.className': '2018'
        }, function (err, course) {
            if (err) {
                console.log('Error in updateDate: ' + err);
            } else {
                let classesHeld = course.local.classesHeld;
                if (classesHeld.indexOf(today) === -1) {
                    Admin.findOneAndUpdate({
                        'local.className': '2018'
                    }, {
                        $push: {
                            'local.classesHeld': today
                        }
                    },{new:true},function(err,obj){
                        if(err){
                            console.log('update error: '+err);
                        }else{
                            console.log('Returned from db: '+obj);
                        }
                    });
                }
            }
        });
    } catch (error) {
        console.log('Error in updateDate: ' + error);
    }
};

I've had this code running on my app for a few days now, and it seems to be working well. Thank you to everyone who responded on this question, I chose my own answer just because it is a complete working solution, but I borrowed from multiple answers on this thread to come up with it.

Upvotes: 0

cheekujha
cheekujha

Reputation: 801

Although the following should work

Admin.update({'local.className':'2018'},{$set : {'local.classesHeld' : classesHeld}});

But as its not working for you. You can also try pushing today date directly into local.classesHeld

Admin.update({'local.className':'2018'}, {$push: {'local.classesHeld': today }})

Upvotes: 1

Willem van der Veen
Willem van der Veen

Reputation: 36620

Try:

await Admin.update({'local.className':'2018'},{$set{'local.classesHeld':classesHeld}});

Instead of:

Admin.update({'local.className':'2018'},{'local.classesHeld':classesHeld});

Upvotes: 1

Zeeshan Tariq
Zeeshan Tariq

Reputation: 624

Have you tried to update it like this: Admin.update({'local.className':'2018'},{$set : {'local.classesHeld' : classesHeld}});

You need to use $set operator, Please have a look at this documentation: http://mongoosejs.com/docs/documents.html

Hope it helps :)

Upvotes: 0

Related Questions