Home > OS >  Accepting Data From a Form And Saving It To MongoDB through Mongoose & Express
Accepting Data From a Form And Saving It To MongoDB through Mongoose & Express

Time:11-04

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.

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. I checked this by examining the database with 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. Here is the link:

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  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>

CodePudding user response:

in this line <input type="text" placeholder="Title" id="title" name=”title”> change name=”title” to name="title"

and do it for this line <textarea rows="5" id="body" name=”body” ></textarea> change name=”body” to name="body"

  • Related