Why am I getting an empty array instead of the user object? if/else statement inside of find()

Hi,
I’m building my exercise tracker with node.js and I have a problem I can’t fix myself.
When I set the conditional after the find() function, I get an empty array instead of the user object back.
This doesn’t happen if I put the same code outside the conditional.
Here’s my full glitch:

Here’s the relevant code:

app.post('/api/exercise/new-user/', function (req, res) {
  
  var username = req.body.username;
  var regex = /[a-zA-Z0-9]/gi;
  
  if (regex.test(username) === true && username.length <= 20){
    User.find({username}).then((user) => {
      if (user) {
        res.send(user);
      } else if (!user) {
       var newUser = new User({
         username
       });
        newUser.save().then((doc) => {
          res.send(doc);
        }, (e) => {
          return res.status(400).send(e);
        }); 
      }
    });
  } else {
    return res.status(400).send('Invalid username');
  }
});

Thanks in advance!!!

I’m still pretty new, but doesn’t an object need an identifier and the data? So shouldn’t it be something like:

   var newUser = new User({
     username: "new name";
   });

edit: missing “;”

Nah, under ES6, if you use the format

var newUser = new User({
  userName
});

it will expand out to

var newUser = new User({
  username: username
});

Bear in mind, the username exists, even if the user doesn’t – take a look where that gets parsed out of the req.body. That’s not the problem. Going to look into it a bit more, but I wanted to correct your point, @Ruben – under anything PRE-ES6, you’d be dead right.

out of curiousity, have you tried logging user to the console on the server at that point?

Still working trough es6 :grin:

there are a LOT of challenges to ES6, but it won’t be going away. There are a lot of features to it, some cosmetic and some functional, but it truly does make javascript considerably more robust IMO.

And yeah, that one about expanding object properties was something I’d learned about watching funfunFunction on YouTube. Highly recommend, very entertaining and easy to understand.

1 Like

I actually fixed it! Thanks!!
I have an additional problem though, maybe you can hep me sort it out?

here:

app.post('/api/exercise/add/', function (req, res) {
  var userId = req.body.userId;
  var description = req.body.description;
  var duration = req.body.duration;
  if (!req.body.date) {
      var date = Date.now()
  } else {
      date = new Date(req.body.date);
    }
  var newExercise = new Exercise({
    userId,
    description,
    duration,
    date
  });
  newExercise.save().then((doc) => {
    res.send(doc);
  }, (e) => {
    res.send(date);
    //return res.status(400).send(e);
  }); 
});

I’m using res.send(date) to see what gets saved when I pass in a date, and for some reason I keep getting null at best, or at “CastError” from mongo at worst. I’m obviously no getting a valid date, but why?

First thing I might recommend, move var date to the beginning of the function. in your if/else branch, you set var date in one, but date is never initialized in the other branch. Shouldn’t necessarily matter, but it’s messy at best.

And rather than using res.send(date), have you tried console.logging both date and req.body.date to see what is triggering the fork AND what the fork is returning?

I don’t see anything in the console when I use console.log anywhere in the code :thinking: that’s why I’ve been using res.send.

Sorry, when you console.log within node, you’ll see it in a console on the server. Is your node running locally, or on something like codepen?

I’m on glitch! it’s a freecodecamp project.

Glitch have a console. In the top left corner you have a log button. That’s where you will see the logs.

SWEET! @SpaniardDev, I haven’t really used Glitch, that’s a great thing to know. Thanks!

No problem :slight_smile: @VirginiaBalseiro have you tried using this.date?

Using the console, and remixed your Glitch to a new one (so I could see the console), I added two lines: one to show the req.body.date string, and one to show the created date. Works fine from what I’m seeing. If the system creates the date, though, it creates a really long number string, the number of milliseconds since Jan 1, 1970. You may want to format that a little prettier.

oh thanks!!! and thanks @SpaniardDev, I totally missed the logs button!!
I’ll keep working on this, will probably bother you once again later if I get stuck :slight_smile: