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');
}
});
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.
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.
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?
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