You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Mongoose Schema数据提交异常:表单提交后部分数据未存入MongoDB

Troubleshooting Missing Fields in Mongoose/MongoDB After Form Submission

Let's break down the most common culprits when some fields from your form aren't showing up in your MongoDB collection, and how to fix them step by step:

1. Verify Exact Field Name Matches (Case & Spelling)

Mongoose and MongoDB are case-sensitive when it comes to field names. Double-check that every name attribute in your form inputs exactly matches the field names defined in your myprojectsSchema.

For example:

  • If your form has an input with name="ClientABN" but your Schema uses clientabn, that field will never be saved.
  • Typos like pocode vs postcode will also cause missing data.

2. Confirm the Frontend Is Actually Sending All Fields

Before blaming Mongoose, check what data your server is receiving from the form. In your submission route (assuming you're using Express), log the request body to see if all fields are present:

app.post('/save-project', (req, res) => {
  console.log('Received form data:', req.body); // Inspect this output carefully
  const newProject = new Myprojects(req.body);
  
  // Rest of your save logic
});

If a field is missing from req.body, the problem is on the frontend:

  • The input might not have a name attribute (critical for form submissions).
  • The input could be disabled, hidden, or not included in the form's submit action.

3. Add Default Values to Your Schema (Optional but Useful)

Right now, your Schema defines fields as plain String types. If the frontend doesn't send a value for a field, Mongoose won't include that field in the saved document at all. To ensure every field exists (even as an empty string), update your Schema with default values:

var myprojectsSchema = new Schema({
  pname: { type: String, default: '' },
  pnumber: { type: String, default: '' },
  plocation: { type: String, default: '' },
  pclient: { type: String, default: '' },
  clientabn: { type: String, default: '' },
  contname: { type: String, default: '' },
  contnumber: { type: String, default: '' },
  mobile: { type: String, default: '' },
  address: { type: String, default: '' },
  city: { type: String, default: '' },
  country: { type: String, default: '' },
  pocode: { type: String, default: '' },
  pdesc: { type: String, default: '' },
  punit: { type: String, default: '' },
  pqty: { type: String, default: '' },
  prate: { type: String, default: '' },
  pprice: { type: String, default: '' },
});

This way, even if a field isn't submitted, it will appear as an empty string in your MongoDB document instead of being missing.

4. Check for Middleware or Hooks That Might Remove Fields

If you've added any pre-save hooks or Express middleware, make sure they aren't accidentally deleting or modifying fields. For example, a faulty pre-save hook like this would strip out the plocation field:

myprojectsSchema.pre('save', function(next) {
  // Accidental deletion - avoid this!
  delete this.plocation;
  next();
});

Also, confirm your body-parsing middleware is configured correctly (for Express 4.16+, use app.use(express.urlencoded({ extended: true })) to ensure all form data is parsed properly).

5. Validate You're Looking at the Correct MongoDB Collection

Mongoose automatically pluralizes and lowercases your model name to create the collection. Since your model is named Myprojects, the collection in MongoDB will be myprojects. Double-check that you're querying the right collection (not a similar-named one like projects).

6. Add Error Handling to Your Save Logic

Always capture errors when saving documents—they might reveal hidden issues like type mismatches or validation failures:

newProject.save()
  .then(savedDoc => {
    console.log('Saved document:', savedDoc); // Check the saved output here too
    res.status(200).send('Project saved successfully');
  })
  .catch(err => {
    console.error('Error saving project:', err); // Log any errors here
    res.status(500).send('Failed to save project');
  });

Even if you don't have explicit validation rules, Mongoose might throw errors if it can't coerce a submitted value to the String type.

Start with steps 1 and 2—those are the most common causes of missing fields. Once you confirm the data is reaching your server correctly, move on to the other checks.

内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:19:49