当所有输入都到位时,保存mongoose模型验证失败

cyvaqqii  于 2023-08-06  发布在  Go
关注(0)|答案(2)|浏览(71)

我无法保存与FormUser模型有一对一Map的submission模型对象。
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, requestingUsername) => {
    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
    }
}


上面的submission.service总是落在catch块中,出现以下错误:

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


我还控制台记录的用户和表单对象下面,他们有所有的数据存在,但仍然发生验证错误。

========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
}


我已经尝试了更多的方法来保存提交对象,搜索stackoverlfow问题,但它不保存。我做错了什么?

7kqas0il

7kqas0il1#

也许是因为你用了new Submission 2次!你可以试试:

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
    }
}

字符串

q7solyqu

q7solyqu2#

为什么使用requestingUsername而不是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
    }
}

字符串

相关问题