/schemas/cart.js

const mongoose = require('mongoose');

const { Schema } = mongoose;
const cartSchema = new Schema({
    goodsId: {
        type: Number,
        required: true,
        unique: true,

    },
    quantity: {
        type: Number,
        require: true,
    }
});

module.exports = mongoose.model("Cart", cartSchema);

routers/goods.js

const Cart = require("../schemas/cart");  // import

router.post("/goods/:goodsId/cart", async (req, res) => {
  const { goodsId } = req.params;
  const { quantity } = req.body;

  isCart = await Cart.find({ goodsId }); // why no declaration?

  console.log(isCart, quantity);

  if (isCart.length) {
    await Cart.updateOne({ goodsId }, { $set: { quantity } });
  } else {
    await Cart.create({ goodsId: goodsId, quantity: quantity });
  }

  res.send({ result: "success" });

})

views/detail.ejs

function addCart() {
  $.ajax({
    type: "POST",
    url: `/api/goods/${goodsId}/cart`,
    data: {
      quantity: sessionStorage.getItem("orderNum")
    },
    error: function(xhr, status, error) {
      if (status == 400) {
        alert("존재하지 않는 상품입니다.");
      }
      window.location.href = "/goods";
    },
    success: function(response) {
      if (response["result"] == "success") {
        $("#cartModal").modal("show");
      }
    }
  });
}

/views/cart