Sails JS单向关联创建标签时出现404错误求助
Let's walk through the likely issues and fixes step by step—this 404 error almost always ties back to a misconfigured model association or an incorrect request route.
1. Fix Your Model Associations
Looking at your current code, the relationship setup doesn’t match the one-to-many logic (one manufacturer has multiple tabs). Here’s what’s wrong:
- In
Manufacturers.js, you’re usingmodel: 'manufacturer_tabs'which is for one-to-one relationships. For multiple tabs per manufacturer, you need acollectionassociation paired with aviafield to link back to the parent manufacturer. - In
Manufacturer_tabs.js, there’s no field tying each tab to its corresponding manufacturer. Without this, Sails can’t associate new tabs with existing manufacturers, and the required nested route won’t function.
Here’s the corrected code for both models:
Manufacturers.js
module.exports = { attributes: { manufacturer_name: { type: 'string' }, manufacturer_logo_url: { type: 'string' }, manufacturer_archived_status: { type: 'boolean' }, // One manufacturer has many tabs (collection + via) manufacturer_tabs: { collection: 'manufacturer_tabs', via: 'manufacturer' // Matches the new field in Manufacturer_tabs } } };
Manufacturer_tabs.js
module.exports = { attributes: { tab_name: { type: 'string' }, // Each tab belongs to one manufacturer manufacturer: { model: 'manufacturers' }, // Update this to a collection if one tab has many fields (same association logic) manufacturer_fields: { collection: 'manufacturer_fields', via: 'manufacturer_tab' } } };
2. Use the Correct Request Endpoint
If you’re using Sails’ default blueprint routes (enabled by default), the proper endpoint to create a tab for a specific manufacturer is a nested route:
POST /manufacturers/:manufacturerId/manufacturer_tabs
Replace :manufacturerId with the actual ID of the manufacturer you just created, and include tab_name in your request body. A 404 often happens if you’re using an invalid route (like /manufacturers/tabs instead of the nested path) or forgetting to specify the manufacturer ID.
3. Confirm Blueprint Routes Are Enabled
Check your config/blueprints.js file to ensure auto-generated routes are turned on. Verify these settings are set to true:
module.exports.blueprints = { actions: true, rest: true, shortcuts: true };
If these are disabled, the nested routes needed to link tabs to manufacturers won’t exist, triggering a 404.
4. Check Model Naming Consistency
Sails uses the model’s lowercase, underscored identifier for routing. Since your model file is Manufacturer_tabs.js, the identifier is manufacturer_tabs—make sure your route uses this exact string (avoid camelCase like manufacturerTabs).
内容的提问来源于stack exchange,提问作者Rahul Dagli

