Now, if we click a goods from index page, detail page shows different item.
views/detail.ejs
function get_detail() {
$.ajax({
type: "GET",
url: `/api/goods/${goodsId}`,
data: {},
error: function(xhr, status, error) {
if (status == 404) {
alert("not exist");
}
window.location.href = "/goods";
},
success: function(response) {
let goodsDetail = response['detail'];
$("#goodsUrl").attr("src", goodsDetail["thumbnailUrl"]);
$("#goodsName").text(goodsDetail["name"]);
$("#goodsPrice").text("$" + number2decimals(goodsDetail["price"]));
sessionStorage.setItem("goodsId", goodsId);
sessionStorage.setItem("goodsName", goodsDetail["name"]);
sessionStorage.setItem("goodsPrice", goodsDetail["price"]);
sessionStorage.setItem("orderNum", 1);
}
})
}
+) this "GET" method is from...
routers/goods.js (already made)
router.post('/goods', async (req, res) => {
const { goodsId, name, thumbnailUrl, category, price } = req.body; // assign seperately
isExist = await Goods.find({ goodsId }); // why doesn't this has "let" or "const"?
if (isExist.length == 0) { // if not duplicated
await Goods.create({ goodsId, name, thumbnailUrl, category, price });
}
res.send({result: "success"});
});