'Getting an empty response from express+NodeJS with POST request
Learning nodeJS so pardon the beginner question I would like to create API endpoints for methods to post and get data from my server.
However, whenever I try to create a new entry with these endpoints on postman my response only returns the id associated with the entry, whereas I should be receiving the id,name and bio. Secondly, I have a validator that should check whether or not a name and bio have been entered before returning a response (if either of these have not been entered, the response should be an error message). When I added my if else statements I only receive the error message regardless of whether I sent a bio and name
Here is my code (model.js):
const insert = ({ name, bio }) => {
const newUser = { id: getId(), name, bio }
users.push(newUser)
return Promise.resolve(newUser)
}
and my endpoints
const express = require('express')
const Model = require('./users/model')
const server = express();
server.use(express.json());
server.post('/api/users', (req, res) => {
let { name, bio } = req.body;
let model = req.body;
if(name && bio) {
Model.insert(name,bio)
.then(elem => {
res.status(201).json(model);
})
}
else {
console.log(name)
res.status(400).json({ message: "Please provide name and bio for the user"})
}
})
FYI: My console.log returns undefined
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
