Home > Back-end >  API receiving undefined data
API receiving undefined data

Time:09-21

I'm new to JS and react, but I'm attempting to pass a string to my API which will then run analysis on said string and pass back a sentiment score. However when I pass the data, whilst in my API's console the string appears in the 'body', the API states that the 'Description' is undefined, causing the API to throw an error.

api.js

const express = require('express');
const app = express()

app.use(express.json());

app.post('/', function (req, res) {
    console.log('body is:', req.body);
    const {description} = req.body;
    console.log('Description is:', description);

    var Analyzer = require('natural').SentimentAnalyzer;
    var stemmer = require('natural').PorterStemmer;
    var analyzer = new Analyzer("English", stemmer, "afinn");
    const sentiment = analyzer.getSentiment(description.split(' '));

    console.log('Sentiment is:', sentiment);

    res.json(JSON.stringify({sentiment}))
})

app.listen(3000, () => {
    console.log('listening on port 3000');
});

diary.js containing the post function and the api call

async function makePostRequest (diaryText) {

  let payload = { Description: diaryText };

  let res = await axios.post('http://localhost:3000/', payload);

  let data = res.data;
  console.log("This is the data returned in the post function:",data);
}

makePostRequest("testText").then(response => {
  console.log("Data returned from the function call:", response);
}).then(error => {
  console.error(error.response.data);
});

This is the output in the console when the above code is executed:

body is: { Description: 'testText' }
Description is: undefined
TypeError: Cannot read property 'split' of undefined
    at /Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/api.js:14:54
    at Layer.handle [as handle_request] (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/layer.js:95:5)
    at next (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/layer.js:95:5)
    at /Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index.js:335:12)
    at next (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/express/lib/router/index.js:275:10)
    at /Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/body-parser/lib/read.js:130:5
    at invokeCallback (/Users/ddavies/Desktop/Desktop Storage/programming/MSc_App/node_modules/raw-body/index.js:224:16)

CodePudding user response:

Object key is sensitive, Description =/= description

const {Description} = req.body;
  • Related