Failed to save items in MongoDB

0

I'm having a problem. When I enter data in an HTML and send it to the MongoDB database, the empty elements are sent to me, that is, the attributes that are saved do not contain the data I enter in the html. Here I show you the example:

addItem = function(req, res) {
 console.log('POST');
 console.log(req.body.nid);
 console.log(req.body.valor);

 var myData = new Item({
    nid:    req.body.nid,
    valor:    req.body.valor
 });

 myData.save().then(item => {
    res.send("item saved to database");
 })

 res.send(myData);}

Here you can see my html:

 <body>
     <h1>Into to Node and MongoDB</h1>
     <form method="post" action="/item">
         <label>Inserte los datos</label><br>
         <input type="text" name="nid" id="nid" placeholder="Enter NID" required>
         <input type="text" name="valor" id="valor" placeholder="Enter VALOR" required>
         <input type="submit" value="Add Name">
     </form>       
 </body>

The outline of the object I keep in the database is as follows:

    var itemSchema = new Schema({
        nid:        { type: String },
        valor:      { type: String }
   });

On the other hand I have the database created in local and as you can see these are the objects that are saved to me when executing a form sending:

    > db.items.find()
    { "_id" : ObjectId("5acca9569b59cf26e7b1e531"), "__v" : 0 }
    { "_id" : ObjectId("5acca95a9b59cf26e7b1e532"), "__v" : 0 }
    { "_id" : ObjectId("5acca9c5fd2b102aac692f39"), "__v" : 0 }
    { "_id" : ObjectId("5accab52e7cc932cb51a67ad"), "__v" : 0 }
    { "_id" : ObjectId("5accab56e7cc932cb51a67ae"), "__v" : 0 }
    { "_id" : ObjectId("5accab72e7cc932cb51a67af"), "__v" : 0 }
    { "_id" : ObjectId("5accab8fe7cc932cb51a67b0"), "__v" : 0 }
    { "_id" : ObjectId("5accabb5e7cc932cb51a67b1"), "__v" : 0 }
    { "_id" : ObjectId("5accabc0e7cc932cb51a67b2"), "__v" : 0 }
    { "_id" : ObjectId("5accabe18e815d2eb13969e6"), "__v" : 0 }
    { "_id" : ObjectId("5accabea8e815d2eb13969e7"), "__v" : 0 }
    { "_id" : ObjectId("5accac38946e422ec6312fe6"), "__v" : 0 }
    { "_id" : ObjectId("5accac757417922eda570a3b"), "__v" : 0 }
    { "_id" : ObjectId("5accacc8d0f18130c5df8f0b"), "__v" : 0 }
    { "_id" : ObjectId("5accaff13281d731777f0196"), "__v" : 0 }
    { "_id" : ObjectId("5accb13426572b337fd0f64b"), "__v" : 0 }

I hope that someone can help me, and thanks in advance, any modification or advice on your part will be of great help for me to continue learning and practicing.

    
asked by Darwin Gonzalez 10.04.2018 в 14:59
source

1 answer

0

Make sure the properties req.body.nid and req.body.valor contain data. In case you try to save any property whose value is undefined, MongoDB will not create it. Check the value of these properties and in case they are undefined, check that you are using the body-parser library, since otherwise the body of the HTTP request will not be available in req.body Another thing that may be happening is that you do not send the data in the correct format to the server, make sure that if the body-parser is configured to parse JSON, you send the data in this format.

    
answered by 10.04.2018 в 16:31