I am doing the URL shortener exercise, and it’s going okay. I’m having a bit of trouble with saving entries to MongoDB, however. Here’s my (relevant) code.
//Initializing the urlSchema for use later
const schema = mongoose.Schema;
const urlSchema = new schema({
original_url: {type: String, required: true},
short_url: {type: Number, required: true}
})
//sending Post data to the server, and saving to MongoDb
app.post("/api/shorturl/new", function(req, res) {
var newEntry = new URL({
original_url: req.body.url,
short_url: Math.floor(Math.random() * 1000) + 1
});
newEntry.save(function(err, doc){
if(err) res.json(err);
else res.send('Successfully inserted!');
});
res.send(req.body.url)
});
Couple things to note. Yes, I know that it’s not returning a JSON object with the new URL and the original url. I can do that, but I’d rather pull them from MongoDB.
I am 99% sure I’m connected properly, my connection link is saved in the env file, and I am using mongoose.connect(process.env.MONGO_URI)
Any ideas why this isn’t working?