應該很簡單,但我遇到了障礙。
表格盡可能簡單。只有一個標題欄位和一個正文欄位。
我希望從看起來像這樣的表單中取回一個物件:
{title:'The Title', body:'This is some body text'}
或者也許這個:
{'title':'The Title', 'body':'This is some body text'}
相反,我得到的是這樣的:
{ '”title”': 'The Title', '”body”': 'This is some body text' }
物件鍵 title 和 body 用單引號和雙引號括起來。
保存到資料庫的物件如下所示:
{ _id: new ObjectId("6182e9ed83c9bbfd6e753ac5"), __v: 0 }
換句話說,保存到資料庫的檔案只有一個 ObjectId,沒有別的。我通過使用 Compass 檢查資料庫來檢查這一點。
然后我對我想要的物件進行了硬編碼,覆寫了我從表單中收到的內容。這導致這被保存在資料庫中:
{
title: 'The Title',
body: 'This is some body text',
_id: new ObjectId("6182ea6b2b5a9c8ea3324668"),
__v: 0
}
所以問題顯然是用單引號和雙引號將物件鍵括起來。我花了一整個下午研究這個,但我被卡住了。
然后我遇到了另一個問題。無論我多么努力,我都無法發布問題,因為我不斷收到以下訊息:
您的代碼格式似乎不正確
三個小時后我放棄了。您可以在 PDF 中找到我的代碼。鏈接在這里:
https://drive.google.com/file/d/1ShfIKNXwEHBuxJ_uBGEf9s1jxb-DripB/view
這是非常簡單的教科書內容。你直接到了底部,看到了快遞后編碼。
幫助!!!!!
客戶端代碼
<!DOCTYPE html>
<html lang="en">
<%-include('layouts/header');-%>
<body>
<!-- Navigation-->
<%-include('layouts/navbar');-%>
<!-- Page Header-->
<header style="background-image: url('assets/img/contact-bg.jpg')">
<div >
<div >
<div >
<div >
<h1>Create New Post</h1>
<span >Have questions? I have answers.</span>
</div>
</div>
</div>
</div>
</header>
<!-- Main Content-->
<div>
<form action="/posts/store" method="POST">
<label>Title</label><br>
<input type="text" placeholder="Title" id="title" name=”title”>
<br>
<label>Body</label><br>
<textarea rows="5" id="body" name=”body” ></textarea>
<br>
<button type="submit" >Send</button>
</form>
</div>
<!-- (all formatting removed from form)--->
<!-- Footer-->
<%-include('layouts/footer');-%>
<!-- Bootstrap core JS-->
<%-include('layouts/scripts');-%>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<!-- Core theme JS-->
<script src="js/scripts.js"></script>
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-->
<!-- * * SB Forms JS * *-->
<!-- * * Activate your form at https://startbootstrap.com/solution/contact-forms * *-->
<!-- * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *-->
<script src="https://cdn.startbootstrap.com/sb-forms-latest.js"></script>
</body>
</html>
uj5u.com熱心網友回復:
在這一行
<input type="text" placeholder="Title" id="title" name=”title”>
更改name=”title”為name="title"
并為此行<textarea rows="5" id="body" name=”body” ></textarea>
更改name=”body”為name="body"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/348833.html
標籤:javascript 节点.js MongoDB 表达 猫鼬
上一篇:ExpressRoutes-錯誤[ERR_HTTP_HEADERS_SENT]:發送到客戶端后無法設定標頭
下一篇:如何在貓鼬中更新陣列中物件的屬性
