jmath412
jmath412

Reputation: 509

Attribute not submitting to MongoDB (MERN stack)

I'm working on an app that assesses students algebra 1 level. I'm trying to send a string called "answers" to the database, but nothing is ever sent. I've shown the model/schema below, where basically each submission should send the answers String (it was originally an object, but I couldn't get an answer to Mongoose not persisting object so I'm just trying a string to see if it even submits a string. The user and date are submitted to the database, but there is not even an entry for the answers attribute. I've seen that the payload sent if I submit a "2" is {"results": "2"} so there's something in the request body. My response back from the server is {} so I think I'm not destructuring a prop correctly or maybe sending an object unintentionally.. Any ideas as to why no answers attribute is submitted to the database? Any help is greatly appreciated!

api/algebra1.js (route to send test results)

const express = require('express');
const router = express.Router();
var bodyParser = require('body-parser')
const bcrypt = require('bcryptjs');
const algebra1 = require('../../models/Algebra1');
const jwt = require('jsonwebtoken');
const config = require('config');
const auth = require('../../middleware/auth');

//@route POST api/auth
//@desc Algebra 1 route
//@access Private
var jsonParser = bodyParser.json();

router.post('/', [jsonParser, auth], async (req, res) => {
  const { answers } = req.body;
  try {
    let newTest = new algebra1({
      answers: answers,
      user: req.user.id,
      date: Date.now()
    })
    console.log("body is " + req.body)
    await newTest.save();
    res.json({ answers: answers });

  } catch (err) {
    console.error(err.message);
    res.status(500).send('Server error');
  }
})

module.exports = router;

Algebra1.js (model for Mongoose):

const mongoose = require('mongoose');

const Algebra1Schema = new mongoose.Schema({
  answers: {
    type: String
  },
  user: {
    type: mongoose.Schema.Types.ObjectId,
    ref: 'user'
  },

  date: {
    type: Date,
    default: Date.now
  }
})

module.exports = algebra1 = mongoose.model('algebra1', Algebra1Schema)

submit action (submits results to api/algebra1 route):

export const submit = (results) => async dispatch => {
  try {
    const config = {
      headers: {
        'Content-Type': 'application/json'
      }
    }
    console.log(results);
    const body = JSON.stringify({ results });

    const res = await axios.post('/api/algebra1', body, config);

    dispatch({
      type: QuestionActionTypes.RESET
    })

    dispatch(setAlert('You\;ve finished your assessment!', 'success'));


  } catch (err) {

    console.error(err.message);
   
  }
}

Upvotes: 0

Views: 88

Answers (1)

Hussam
Hussam

Reputation: 1676

You are sending data with results key and destructing as answer key. Where are you sending anything against answer key ? I guess you meant to submit results as answers.

const body = JSON.stringify({ answers: results });

Upvotes: 1

Related Questions