Tell us what’s happening:
I cannot insert a user in my database. I don’t understand why. I’m doing the exact same things I did for the URL shortener, and nothing happens. I’ve inserted the connect uri in the environment variable MONGO_URI.
Your code so far
const mongoose = require("mongoose");
mongoose.connect(process.env.MONGO_URI);
const UserSchema = new mongoose.Schema({
username: String
});
const User = mongoose.model("User", UserSchema);
app.get("/test1", function(req, res) {
const usertest = new User({ username: "usertest1" });
usertest.save();
res.send("Test 1 executed.");
});
As I said, nothing happens. In the logs, I get a deprecation warning:
(node:17818) DeprecationWarning: `open()` is deprecated in mongoose >= 4.11.0, use `openUri()` instead, or set the `useMongoClient` option if using `connect()` or `createConnection()`. See http://mongoosejs.com/docs/4.x/docs/connections.html#use-mongo-client
I’ve tried adding changing the connect command as suggested by the deprecation link like this:
mongoose.connect(process.env.MONGO_URI, {useMongoClient: true});
But it invalidates the connection URI that I get from Atlas, so I dropped it. Apart from this, I don’t know what else to do. Any help is appreciated. I should add that if on the ‘test1’ get request I do res.send(usertest);
, I can see the user object with the ID. But it doesn’t seem to get sent to the database.
Challenge: Exercise Tracker
Link to the challenge: