I noticed everytime I hit Save in my VSCode, even though newUrl
is unchanged, a new document is updated to MongoDB, with a new _id
.
Is this behavior normal? Is it by default? Can I change it?
My code is like this:
require("dotenv").config();
const express = require("express");
const app = express();
const cors = require("cors");
const mongoose = require("mongoose");
const {Schema, model} = mongoose;
mongoose.connect(process.env.MONGO_URI, {useNewUrlParser: true, useUnifiedTopology: true});
const urlSchema = new Schema({
original_url: {
type: String,
required: true,
},
short_url: Number,
});
const Url = model("url", urlSchema);
// unchanged
//
let newUrl = new Url({
original_url: "https://www.google.com/",
short_url: 1,
});
newUrl.save((err, data) => {
if (err) console.error(err);
});
//
//
app.use(cors());
app.get("/", (req, res) => {
res.sendFile(`${__dirname}/views/index.html`);
});
app.listen(3000);