skip to Main Content

I am not able to save submission model object which has one to one mapping with Form and User model.

submission.model.js

const mongoose = require('mongoose')
const Schema = mongoose.Schema
const submissionSchema = new Schema({
    form: {
    type: Schema.Types.ObjectId,
    ref: 'Form'
  },
    user: {
    type: Schema.Types.ObjectId,
    ref: 'User'
  },
    createdAt: {
    type: Date,
    default: Date.now 
  },
  updatedAt: {
    type: Date,
    default: Date.now 
  }
})

module.exports = mongoose.model('Submission', submissionSchema)

submission.service.js

const createSubmission = async (formID, requestingUser) => {
    try {
        const user = await User.findOne({username: requestingUsername})
        const form = await Form.findOne({_id: formID})
        
      const submissionItem = new Submission({
            "form": form._id.toString(), // tried saving direct object / object ID as well
            "user": user._id.toString()
        })

        await submissionItem.save()
        return true
    }
    catch(err){
        console.log("======error", err)  // This piece is always raising error
        return false
    }
}

Above submission.service is always falling in catch block with below error:

Form validation failed: inputField: Path `inputField` is required., description: Path `description` is required., title: Path `title` is required.

I also console logged user and form objects below, they have all the data present, but still validation error occurs.

========user {
  _id: new ObjectId("64c0a5e6621d24a1ae6b3549"),
  username: 'Magnisium',
  password: '$2b$10$K0J0ChyVukctZyW.nfqkJum.vVkltIHzpMEOg5fzp81OAVF4ml40W',
  userType: 'customer',
  createdAt: 2023-07-26T04:49:42.585Z,
  updatedAt: 2023-07-26T04:49:42.586Z,
  __v: 0,
  refreshToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFzaGlzaC5nYW5kaHkiLCJpYXQiOjE2OTAzNTcyMDQsImV4cCI6MTY5MDQ0MzYwNH0.kJhVrpv57QT0oBC0EbvAweGikJSqAWVRkNKi1puOCQI'
}

========form {
  _id: new ObjectId("64c0af37b8b7b433bee328f6"),
  title: 'Peng',
  description: 'dfdfd',
  inputField: 'Something',
  user: new ObjectId("64c0a5e6621d24a1ae6b3549"),
  createdAt: 2023-07-26T05:29:27.944Z,
  updatedAt: 2023-07-26T05:29:27.944Z,
  __v: 0
}

I have tried more ways of saving the submission object, searched stackoverlfow questions, however it is not saving. What am I doing wrong?

2

Answers


  1. Maybe because you used new Submission 2 times!
    you can try it:

    const createSubmission = async (formID, requestingUser) => {
        try {
            const user = await User.findOne({username: requestingUsername})
            const form = await Form.findOne({_id: formID})
            
            const submissionItem = new Submission({
                form: form._id,
                user: user._id
            })
    
            await submissionItem.save()
            return true
        }
        catch(err){
            console.log("======error", err)  // This piece is always raising error
            return false
        }
    }
    
    Login or Signup to reply.
  2. Why are you using requestingUsername instead of requestingUser

    const createSubmission = async (formID, requestingUser) => {
        try {
            const user = await User.findOne({username: requestingUsername}) -> *here*
            const form = await Form.findOne({_id: formID})
            
          const submissionItem = new Submission({
                "form": form._id.toString(), // tried saving direct object / object ID as well
                "user": user._id.toString()
            })
    
            await submissionItem.save()
            return true
        }
        catch(err){
            console.log("======error", err)  // This piece is always raising error
            return false
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search