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

MongoDB+Mongoose+Express.js应用无法新增数据库条目求助

Troubleshooting "There was a problem adding the information to the database." in your Blobs App

Hey there, let's dig into why your blob isn't saving to the database. I've gone through your code snippet and spotted a few clear issues, plus some debugging steps to get to the bottom of this:

1. You're missing the company field in your create call

Looking at your code, you correctly grab var company = req.body.company; from the POST request, but you don't include this field when creating the Blob document:

// Your current create call (missing company)
mongoose.model('Blob').create({
  name : name,
  badge : badge,
  dob : dob,
  isloved : isloved
}, function (err, blob) { ... })

If your Blob Mongoose schema defines company as a required field, this will trigger a validation error—exactly what's causing that generic error message. Fix this by adding company: company to the object:

mongoose.model('Blob').create({
  name : name,
  badge : badge,
  dob : dob,
  company : company, // Add this line!
  isloved : isloved
}, function (err, blob) { ... })

2. Add detailed error logging to see the real issue

Right now, you're only returning a generic error message, which gives you no clue about what's actually broken. Update your error handler to log the full error to your terminal and include the error message in the response:

if (err) {
  console.error('Failed to save blob:', err); // Log the error to your terminal
  res.send(`There was a problem adding the information to the database: ${err.message}`);
}

This will tell you exactly what went wrong—like a missing required field, invalid data type, or database connection issue.

3. Verify your Blob Mongoose schema matches your data

Double-check your Blob schema definition to make sure:

  • All fields you're sending (name, badge, dob, company, isloved) are defined in the schema
  • Data types match what you're passing in (e.g., if dob is a Date type, ensure the frontend sends a valid date string; if isloved is a Boolean, make sure you're not passing a string like "true" instead of a boolean value)
  • No required fields are missing from your create call (we fixed the company field, but check others too!)

4. Confirm body-parser is properly set up

Express doesn't automatically parse POST request bodies—if you haven't configured body-parser, req.body will be undefined, which means all your field variables (name, badge, etc.) will be undefined too. Make sure your app has these lines somewhere near the top (before your routes):

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true })); // For form data
app.use(bodyParser.json()); // For JSON requests

5. Check your MongoDB connection

If Mongoose isn't connected to your MongoDB instance, all database operations will fail. Add logging to your connection code to confirm it's working:

mongoose.connect('your-mongodb-connection-string')
  .then(() => console.log('Successfully connected to MongoDB'))
  .catch(err => console.error('MongoDB connection failed:', err));

If you see a connection error here, fix that first (check your connection string, ensure MongoDB is running, etc.)

Start with fixing the missing company field and adding error logging—those two steps will either solve your problem or give you the exact info you need to fix the next issue.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 09:49:56