2Radon 2Radon - 1 year ago 159
Node.js Question

Post request saving only versionKey with Mongoose and Express

I am trying to save a number and a few string values to a MongoDB and even though the code makes perfect sense to me, returns no errors and creates an entry in the database, all I get is just a versionKey, something like this:

"_id": {
"$oid": "58052711f319bc041c5ebdac"
"__v": 0

I thought I'd try just saving the number and the title first to see if they get saved, but they don't, as you can see above.
Every RESTful API and Express "post" request tutorial and answer I find seems to do it differently!

Also, is it supposed to show the res.json in plaintext for me to format or render somehow, or is it supposed to show the value of the property

Here is the code

// modules =======================================================
const express = require('express')
const app = express()
const mongoose = require('mongoose')

// configuration =================================================
const db = require('./config/db')

// mongoose ======================================================

const PostSchema = new mongoose.Schema({
}, { collection: 'posts' })

const posts = mongoose.model('Post', PostSchema)

// routes ========================================================
app.post('/api/post', function(req, res) {
var post = new posts()
post.number = req.body.number
post.title = req.body.title
post.save(function(err) {
if (err) res.send(err)
res.json({ message: 'Post saved'})

Here is my HTML5 form

<form action="/api/post" enctype="multipart/form-data" method="post">
<label for="number">Post Number:</label>
<input type="number" name="number" size="2" placeholder="required" required /><br />

<label for="title">Title:</label>
<input type="text" name="title" placeholder="optional" /><br />

<label for="body">Text:</label><br />
<textarea name="body" cols="80" rows="20" placeholder="optional"></textarea><br />

<label for="images">Images:</label>
<input type="text" name="images" placeholder="optional" />
<span class="form-hint">Comma separated file names, e.g. image1.jpg,image2.jpg,image3.png</span><br />

<input type="submit" value="Submit" />

My middleware that comes just before the routes in server.js

// middleware ====================================================
app.use(bodyParser.json()) // parse application/json
app.use(bodyParser.json({ type: 'application/vnd.api+json' })) // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })) // parse application/x-www-form-urlencoded
app.use(methodOverride('X-HTTP-Method-Override')) // override with the X-HTTP-Method-Override header in the request
app.use(express.static(__dirname + '/public')) // set static files location

Answer Source

First question, the encoding must match what you have Express parsing (in this case it wasn't multipart/form-data but application/json, 'application/vnd.api+json', and application/x-www-form-urlencoded. Removing the encoding type you were specifying fixes that.

Second, the response will be a simple JSON object:

    "message": "Post saved"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download