Я работаю над своим первым приложением среднего стека и сталкиваюсь с проблемой. У меня есть модель блога, и я пытаюсь назначить свойства объекта req, но он не определен. Когда я делаю консольный журнал req.body, он выглядит так:
{ '{"title":"some title", "body":"some body", "createdBy":" "created By"}':''}
но когда я выписываю значения по отдельности, как console.log(req.body.title)
, это не определено. В моем server.js я включил парсер тела и убедился, что маршрут идет после включения парсера тела. Так что на данный момент я не уверен, почему это было бы неопределенным, любая помощь приветствуется.
Вот пост для блога:
createAuthenticationHeaders() {
this.authService.loadToken();
this.httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/x-www-form-urlencoded',
authorization: this.authService.authToken
})
};
}
newBlog(blog) {
this.createAuthenticationHeaders(); // Create headers
return this.http.post(this.domain + 'blogs/newBlog', blog,
this.httpOptions);
}
Это полезная нагрузка

Спасибо
Вот этот файл
const express = require('express');
const app = express();
const router = express.Router();
const mongoose = require('mongoose');
const config = require('./config/database');
const path = require('path');
const authentication = require('./routes/authentication')(router);
const blogs = require('./routes/blogs')(router);
const bodyParser = require('body-parser');
const cors = require('cors');
const port = 8080;
mongoose.connect(config.uri, err => {
if (err) {
console.log('Connection error db ', err);
} else {
console.log('Connected to db ', config.db);
}
});
app.use(
cors({
origin: 'http://localhost:4200'
})
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/client/dist/'));
app.use('/authentication', authentication);
app.use('/blogs', blogs);
app.get('*', (req, res) => res.sendFile(__dirname +
'/client/dist/index.html'));
app.listen(port, () => console.log(`App listening on port ${port}`));