NodeJS-ExpressJS-Mongoose POST API外键关联报错:Cast to ObjectID失败
Hey there! Let's break down why you're hitting that ObjectID cast error and how to fix it right away.
What's Causing the Error?
This error pops up because Mongoose expects your adress field to be an ObjectID (a reference to an existing Address document), but you're sending a full nested Address object instead. Your Business model is currently set up to link to a separate Address collection, not store Address data directly inside the Business document.
Two Solutions Based on Your Use Case
1. If You Want Address as an Embedded Subdocument
If Address doesn't need to exist as its own standalone collection (you just want it tied directly to a Business), adjust your Mongoose models to embed the Address schema:
const mongoose = require('mongoose'); const Schema = mongoose.Schema; // Define the Address schema const AddressSchema = new Schema({ street: String, number: String, city: String, postalcode: String, country: String }); // Define Business with embedded Address const BusinessSchema = new Schema({ name: String, adress: AddressSchema // Embed the schema directly, no reference }); module.exports = mongoose.model('Business', BusinessSchema);
With this setup, your original POST request JSON will work perfectly—Mongoose will store the Address data as a nested part of the Business document. Just make sure the field name adress matches exactly across your request, model, and code (you're missing an 'd' compared to standard "address", which is totally fine as long as it's consistent everywhere).
2. If You Want Address as a Separate Collection (Reference)
If you need Address to be its own independent collection, you'll need to create an Address document first, then use its ObjectID when creating a Business.
First, here's how your models should look:
// Address model (separate collection) const AddressSchema = new Schema({ street: String, number: String, city: String, postalcode: String, country: String }); module.exports = mongoose.model('Address', AddressSchema); // Business model (references Address) const BusinessSchema = new Schema({ name: String, adress: { type: Schema.Types.ObjectId, ref: 'Address' // Links to the Address collection } }); module.exports = mongoose.model('Business', BusinessSchema);
Then, follow these steps:
- Create and save an Address document to get its unique ObjectID
- Use that ID in your Business POST request
Example backend code:
const Address = require('./models/Address'); const Business = require('./models/Business'); // First create the Address const newAddress = new Address({ street: "test", number: "5", city: "leuven", postalcode: "2900", country: "BEL" }); await newAddress.save(); // Then create the Business with the Address ID const newBusiness = new Business({ name: "GRIT", adress: newAddress._id // Pass the ObjectID here }); await newBusiness.save();
If sending from a client, first call your Address creation endpoint to get the ID, then send this JSON for Business:
{ "name" :"GRIT", "adress": "5ad8b5e4ac4b1a443877bfc2" }
Quick Check
Don't forget to verify the spelling of adress across all your code and requests—typos like this can cause weird issues even when everything else is set up correctly.
内容的提问来源于stack exchange,提问作者Bram Sikkens

