[英]Accepting Data From a Form And Saving It To MongoDB through Mongoose & Express
Should be simple but I've hit a snag.应该很简单,但我遇到了障碍。
The form is as simple as possible.表格尽可能简单。 Just a title field and a body field.
只有一个标题字段和一个正文字段。
I expected to get an object back from the form that looked something like this:我希望从看起来像这样的表单中取回一个对象:
{title:'The Title', body:'This is some body text'}
Or maybe this:或者也许这个:
{'title':'The Title', 'body':'This is some body text'}
Instead what I got back is this:相反,我得到的是这样的:
{ '”title”': 'The Title', '”body”': 'This is some body text' }
The object keys, title & body, are enclosed in both single and double quotes .对象键 title 和 body 用单引号和双引号括起来。
The object that was saved to the database looked like this:保存到数据库的对象如下所示:
{ _id: new ObjectId("6182e9ed83c9bbfd6e753ac5"), __v: 0 }
In other words, the document that was saved to the database had an ObjectId and nothing else.换句话说,保存到数据库的文档只有一个 ObjectId,没有别的。 I checked this by examining the database with Compass.
我通过使用 Compass 检查数据库来检查这一点。
I then hardcoded the object I wanted, overwriting what I had received from the form.然后我对我想要的对象进行了硬编码,覆盖了我从表单中收到的内容。 That resulted in this being saved on the database:
这导致这被保存在数据库中:
{
title: 'The Title',
body: 'This is some body text',
_id: new ObjectId("6182ea6b2b5a9c8ea3324668"),
__v: 0
}
So the problem clearly is enclosing object keys in both single and double quotes.所以问题显然是用单引号和双引号将对象键括起来。 I spent a whole afternoon researching this but I'm stuck.
我花了一整个下午研究这个,但我被卡住了。
I then encountered another problem.然后我遇到了另一个问题。 No matter how hard I tried I was unable to post the question because I kept getting a message along the following lines:
无论我多么努力,我都无法发布问题,因为我不断收到以下消息:
You appear to have incorrectly formatted code您的代码格式似乎不正确
After three hours I gave up.三个小时后我放弃了。 You can find my code in a PDF.
您可以在 PDF 中找到我的代码。 Here is the link:
链接在这里:
https://drive.google.com/file/d/1ShfIKNXwEHBuxJ_uBGEf9s1jxb-DripB/view https://drive.google.com/file/d/1ShfIKNXwEHBuxJ_uBGEf9s1jxb-DripB/view
It's very straightforward textbook stuff.这是非常简单的教科书内容。 You got straight to the bottom and see the express post coding.
你直接到了底部,看到了快递后编码。
HELP!!!!!帮助!!!!!
CLIENT SIDE CODE客户端代码
<!DOCTYPE html>
<html lang="en">
<%-include('layouts/header');-%>
<body>
<!-- Navigation-->
<%-include('layouts/navbar');-%>
<!-- Page Header-->
<header class="masthead" style="background-image: url('assets/img/contact-bg.jpg')">
<div class="container position-relative px-4 px-lg-5">
<div class="row gx-4 gx-lg-5 justify-content-center">
<div class="col-md-10 col-lg-8 col-xl-7">
<div class="page-heading">
<h1>Create New Post</h1>
<span class="subheading">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" class="form-control" 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/bootstrap@5.1.3/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>
in this line <input type="text" class="form-control" placeholder="Title" id="title" name=”title”>
change name=”title”
to name="title"
在这一行
<input type="text" class="form-control" placeholder="Title" id="title" name=”title”>
将name=”title”
更改为name="title"
and do it for this line <textarea rows="5" id="body" name=”body” ></textarea>
change name=”body”
to name="body"
并对这一行进行操作
<textarea rows="5" id="body" name=”body” ></textarea>
将name=”body”
更改为name="body"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.