Hey everyone! I’m currently working on the Exercise Log project (here’s my Glitch project), and I’m very confused as to what is going on when I call .save() on a Mongoose model. You can see the full context on Glitch, but here is the specific part that’s not behaving as I’d expect it to:
app.post("/api/exercise/new-user", (req, res) => {
var newUser = new User({ name: req.body.username });
newUser.save((error) => console.error(error));
res.json({username: newUser.name, _id: newUser._id})
});
I’m seeing the JSON response in my browser with the information I’d expect, but I’m not seeing a new document in my database on MongoDB. What am I missing here?
And a broader question that I have that I didn’t feel like was explained in the curriculum is at what point is a collection created? Is it when I create a new model? Like at this point in my code:
const mongoose = require("mongoose");
exports.User = mongoose.model(
"User",
new mongoose.Schema({
name: { type: String, required: true }
})
);
Or does it happen when I go to save an instance of the model? When is MongoDB told to create the collection? Thanks very much in advance!