Postman post request cannot read property of undefined

postman cannot read property 'set' of undefined
cannot read property 'sigbytes' of undefined postman
typeerror: cannot read property 'length' of undefined postman
cannot read property of undefined node js
typeerror: cannot read property of undefined
typeerror: cannot read property 'body' of undefined
postman dynamic variables in pre request script
postman typeerror: cannot read property 'access_token' of undefined

I have a route setup in my express server which sends a post request and adds a new message o my mongoDB database.

When I statically add names to the body fields; i.e new userMessage = new Message({ name: 'Joseph', email: 'joseph@gmail.com', message: 'Hello Joseph'}), it posts successfully.

But when it is dynamic; i.e new userMessage = new Message({ name: req.body.name, email: req.body.email, message: req.body.message}) it throws an error.

I have tried several times to debug the issue with no success.

Error

TypeError: Cannot read property 'name' of undefined
    at router.post (C:\Users\Joseph\Documents\Hackathons\hack.api\app\routes\message.js:13:24)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\express\lib\router\index.js:275:10)
    at cors (C:\Users\Joseph\Documents\Hackathons\hack.api\app\node_modules\cors\lib\index.js:188:7)

Message.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const MessageSchema = new Schema({
    name: { type: String, required: true },
    email: { type: String, required: true },
    message: { type: String, required: true }
});

module.exports = Message = mongoose.model('message', MessageSchema);

Server.js

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const cors = require('cors');

const message =  require('../routes/message');

const app = express();

//Enable cors
app.use(cors( {origin: '*' }));

//Connect to mongoose
mongoose.connect('mongodb://localhost/messages', { useNewUrlParser: true })
    .then(() => console.log('MongoDB connected'))
    .catch(err => console.log(err));

//Use routes
app.use('/message', message);

//Body Parser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

//Test route
app.get('/', (req, res) => {
    res.send('Hello world');
})

//Set server port
const port = process.env.PORT || 5000;

app.listen(port, () => {
    console.log(`Server started on port ${port}`);
});  

message.js

const express = require('express');
const router = express.Router();

//Load message model
const Message = require('../models/Message');

router.get('/test', (req, res) =>
    res.json({ msg: 'Message post route works' })
);

router.post('/post', (req, res) => {
    const userMessage = new Message({
        name: req.body.name,
        email: req.body.email,
        message: req.body.message
    });

    userMessage.save()
        .then(message => res.json(message))
        .catch(err => console.log(err))
});

module.exports = router;

I solved it by changing

//Use routes
app.use('/message', message);

//Body Parser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

to

//Body Parser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

//Use routes
app.use('/message', message);

I guess the order matters. In server configuration order really is important.

Postman post request cannot read property of undefined, I solved it by changing //Use routes app.use('/message', message); //Body Parser middleware app.use(bodyParser.urlencoded({extended:  Hey @tohlaymui35,. From your example response body - it looks like it’s all in an array so changing your expect statement to jsonData[0].message.content[1].data.title would make the test pass.

It's better to get your data and validate it before creating new Mongo object:

const { body: {name, email, message } } = req;
//validate email for example or you just logs it to know data reviced or not? 
const userMessage = new Message({
        name: name,
        email: email,
        message: message
    });

TypeError: Cannot read property 'content' of undefined, Run the post request in postman GET http://{{hostname}}:8086/cm/x/response?​conversationId={{convoId}} Click the send The body return has the content array​  This is causing the error “TypeError: Cannot read property ‘set’ of undefined”. pm.collectionVariables is undefined as it does not exist yet in your version of Newman. Just using double curly braces in a string (like {{source_bank_code}}) won’t motivate Postman to replace them for you. You can use the pm.* API to fetch the respective value.

If you are sending nested objects from client side like person[name] = 'cw' then you have to write app.use(bodyParser.urlencoded({extended: true})); or if that is not the case,make sure your input in the html form has the name="file" attribute.

TypeError: Cannot read property 'has' of undefined - Help, Those assertions are also the old approach for writing tests in Postman. I'd suggest writing your tests using the pm.test() method, and ChaiJS  I have a route setup in my express server which sends a post request and adds a new message o my mongoDB database. When I statically add names to the body fields; i.e new userMessage = new Message({ name: 'Joseph', email: 'joseph@gmail.com', message: 'Hello Joseph'}), it posts successfully.

Getting error in Postman request as TypeError Cannot read property , Getting error in Postman request as TypeError Cannot read property 'xyz' of undefined - postman-collection-runner. I am trying to make POST call towards Search Console API. I got my example running under API Explorer, however when I  I am executing a collection using Runner on Postman version 7.14.0. As part of the Tests Script, I am validating the response header contents using: tests[“Content-Type is present”] = postman.getResponseHeader(“Content-…

TypeError: Cannot read property of undefined in Node Express / POST, TypeError: Cannot read property of undefined in Node Express / POST - node.js. Just follow the image to run api in postman In log it is printed as shown in In the following code, i.e., in the POST request: app.post('/mobile/registration', (res,  Postman test throws error: TypeError: Cannot read property 'ProjectNumber' of undefined

Test Postman collection First test, avatar image. Question by list.fr-fde · Apr 11 '19 at 4:48 AM · postman collection i found always this Erreur "There was an error in evaluating the Pre-request Script: TypeError: Cannot read property 'sigBytes' of undefined". Could you please  Hey @LayMuiToh, Looks like your response is returning an array of objects and you're not accessing it correctly in your test script. Closing this issue as this is not a bug with Postman itself and we would recommend posting this question on https://community.getpostman.com instead.