我正在構(gòu)建簡短的 Web 應(yīng)用程序,并使用 axios.js 將數(shù)據(jù)發(fā)送到數(shù)據(jù)庫。但是,當(dāng)我從數(shù)據(jù)庫中獲取日期時(shí),會(huì)呈現(xiàn)完整的日期,而我只希望 getDate()、getMonth() 和 getFullYear() 起作用,但它們似乎不起作用。這是 Windows 7 本地主機(jī)服務(wù)器。我已經(jīng)厭倦了用不同的方式稱呼它。function itemTemplate(item) { return `<li class="list-group-item list-group-item-action d-flex align-items-center justify-content-between" style = "background-color: #cff0cc; color: #498a17; font-family: 'Kite One', sans-serif;"> <span class="item-text">Date: ${item.date.getDate()} / ${item.date.getMonth() + 1} / ${item.date.getFullyear()} - ${item.text}</span> <div> <button data-id="${ item._id }" class="edit-me btn btn-primary btn-sm" style="background-color: darkgrey;border-color: grey;"><i class="fa fa-edit" style="width: 10px;"></i></button> <button data-id="${ item._id }" class="delete-me btn btn-danger btn-sm"><i class="fa fa-minus-circle" aria-hidden="true" style="width: 10px;"></i> </button> </div> </li>`;}// Initial Page Load Renderlet ourHTML = items .map(function(item) { return itemTemplate(item); }) .join("");document.getElementById("item-list").insertAdjacentHTML("beforeend", ourHTML);// Create Featurelet createField = document.getElementById("create-field");let createdDate = new Date();document.getElementById("create-form").addEventListener("submit", function(e) { e.preventDefault(); axios .post("/create-item", { text: createField.value, date: createdDate }) .then(function(response) { // Create the HTML for a new item document .getElementById("item-list") .insertAdjacentHTML("beforeend", itemTemplate(response.data)); createField.value = ""; createField.focus(); }) .catch(function() { res.send("Please try again later."); });});預(yù)期的是,這三個(gè)日期函數(shù)的工作,但錯(cuò)誤消息顯示“item.date”在第 6 行未定義
為什么日期函數(shù)沒有返回我預(yù)期的輸出?
SMILET
2021-10-14 13:19:39