Node.js - Sometimes Console.log doesn't work?

Sometimes when I’m doing the Node.js lessons, I don’t really understand what’s happening and I’d like to console.log the data variable to see what’s going on.

For example, this is the answer I got for ‘Create and Save a Record of a Model’ lesson:

console.log ("Hello World!");

var createAndSavePerson = function(done) {
  var newPerson = new Person ({name: 'Bobby', age: 42, favoriteFoods: ['Tacos', 'Pizza', 'Pickles']});
  
  newPerson.save(function(err, data) {
    console.log(data);
    if (err) return done(err)
      return done(null, data);
    });
}

I put 2 console.log commands to see if it worked. The first one returns “Hello World!” in the Glitch Status Logger. The second one, nothing happens.

Is it because the createAndSavePerson is a function? It’s not actually being called, so the code inside the function isn’t actually running just yet? Or am I missing something.

I usually do the error check first…eg)


    newDoc.save((err) => {
      if (err) { 
        console.log('CB err: ', err.errors);
        const errMsg = "Error submitting picture: " + err.errors.imgUrl;
        console.log(errMsg);
        res.render('addpic', { errors: err.errors });
      } 
      else {
        res.redirect('/');
      }
    });

Also, does save take a 2nd parameter? I have used findOneAndUpdate before

1 Like

Correct.
But understand that it’s not that it's not logging anything because it's a function. It’s not logging because the function is never called.

1 Like

@JohnnyBizzel:
I’m sorry, I’m not sure about .save(). I looked in the docs but couldn’t figure it out:
https://mongoosejs.com/docs/api.html#model_Model-save

@adityaparab:
Right, just like in regular JavaScript. If you create a function but never call it, nothing inside the function runs.

I guess that’s part of the reason the Mongoose lessons are a little bit confusing for me:
The lesson asks you to write out these functions that are never called, so the only way I know if my answer’s right is by running the FCC test. :sweat_smile:

I can’t actually gauge myself if it works or not because I don’t know how to call these variable/functions.

save doesn’t get called until you try to save something. When you call createAndSavePerson, in the course of it’s job will call the method save and once that is done, the callback will get called. But it happens in that order and each is dependent on the one that precedes it. But if everything is working correctly, you should eventually log that data. If you’re unsure what is happening, put things like console.log('here 1'); and etc. throughout the code so you can follow it and see where things are firing and where they aren’t.

Yes, it’s confusing. Yes, the docs could be better. But if you keep at it, it will start to make sense. Part of it is also just learning a different way of thinking.

1 Like

Well… It looks like it was a perfect day to publish a new video to my YouTube channel… It’s not specifically related to your questions about mongoose. But it’ll definitely help you gain more understanding of how the functions that are not called but somehow work dilemma.

Hope that helps… :slight_smile:

1 Like