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

Sails JS单向关联创建标签时出现404错误求助

Troubleshooting 404 Error When Creating Tabs for Manufacturers

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 using model: 'manufacturer_tabs' which is for one-to-one relationships. For multiple tabs per manufacturer, you need a collection association paired with a via field 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:51:30