I have my schema like i have userDetials and in userDetails i have this principal Application Detials and in i have salutation field and now i want to enter input using form data in postman how i can do that and this is my schema:
{"selectedOption": "Individual","userDetails": {"principalApplicationDetails": {"salutation": "65fbe8ec74717b561f239821","name": "John Doe","fatherHusbandName": "Doe Sr.","motherName": "Jane Doe","identificationType": "CNIC","identificationNumber": "12345-1231441-5","issuanceDate": "2003-01-01","expiryDate": "2022-01-01","placeOfBirth": {"country": "65f339c5061ae53feb89c09a","province": "65f5e08ca2ebc406402e3efa","city": "65fd14466e03c2bef8cd98d9" },"dateOfBirth": "2003-09-19","Nationality": "65f339c5061ae53feb89c09a","Gender": "Male","Religion": "65fb28c9a52265ff48f5fd2a","zakatDeduction": "Yes","nationalTaxNumber": "1234567890123","taxStatus": "Filer","correspondenceAddress": "123 Main St","mobileNumber": "0316-4251521", // "post": "john.d@example.com""email":"242443424342432" }, }}
this is my key that i am entering in form data in postman: userDetails[principalApplicationDetails][salutation] and this is my value for respective key: 65fbe8ec74717b561f239821 but it is showing me this error: userDetails.principalApplicationDetails.salutation: Path principalApplicationDetails.salutation
is required." and this is my controller code:
import userModel from "../models/userModel.js";import fs from 'fs'export const createUserController = async (req, res) => { try {// console.log(req.fields) // Extract fields from the form data console.log(req.fields) const { selectedOption, userDetails} = req.fields; const { cnicFrontImage, cnicBackImage,salutation } = req.files; // const { principalApplicationDetails } = userDetails; // const { salutation } = principalApplicationDetails; // Validate the input data according to your schema // Handle validation errors // Create a new user instance based on the validated data const newUser = new userModel({ selectedOption, userDetails: { ...userDetails, // principalApplicationDetails: { // ...principalApplicationDetails, // salutation: salutation // Assigning salutation directly // }, cnicFrontImage: { data: fs.readFileSync(cnicFrontImage.path), contentType: cnicFrontImage.type }, cnicBackImage: { data: fs.readFileSync(cnicBackImage.path), contentType: cnicBackImage.type } } }); // Save the new user instance to the database await newUser.save(); // Send success response return res.status(201).json({ success: true, message: 'New user created successfully', user: newUser }); } catch (error) { console.error('Error creating user:', error); // Handle errors and send appropriate response return res.status(500).json({ success: false, message: 'Error creating user', error: error.message }); }};