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

Sails.js中POST API参数自动转为字符串问题求助

Fixing Parameter Type Conversion Issues in Sails.js API

Hey there! I’ve run into this exact problem with Sails.js before, so let’s break down why your parameters are getting forced into strings and how to fix it.

Common Causes & Solutions

1. Incorrect Content-Type in Postman

This is the most likely culprit. If you don’t set the right request header, Sails will default to parsing all incoming values as strings—even numbers and arrays.

How to fix it in Postman:

  • Navigate to the Headers tab of your request.
  • Add a key-value pair: Content-Type: application/json.
  • Switch to the Body tab, select raw, and pick JSON from the format dropdown.
  • Send your payload in valid JSON syntax, like this:
    {
      "pageSize": 2,
      "pageNumber": 1,
      "marketregion": ["abc"],
      "category": "electronics",
      "fromDate": "2024-01-01",
      "toDate": "2024-06-01",
      "group_size": 5
    }
    

After this, req.body should receive pageSize as a number and marketregion as an actual array instead of stringified versions.

2. Misconfigured Body Parser in Sails

Sails relies on body-parser to parse incoming requests. If this middleware isn’t set up to handle JSON or urlencoded data correctly, it might coerce all values to strings.

Check your config/http.js file:
Ensure the bodyParser configuration supports both JSON and urlencoded data. Here’s a standard working setup:

module.exports.http = {
  middleware: {
    bodyParser: (function () {
      const opts = {
        limit: '10mb',
        extended: true
      };
      // Combine urlencoded and JSON parsers
      return require('body-parser').urlencoded(opts)
        .concat(require('body-parser').json(opts));
    })(),
    // ... other middleware configurations
  }
};

This setup ensures Sails correctly parses data while preserving its original type.

3. Manual Type Conversion (Quick Temporary Fix)

If you can’t adjust the request format right away, you can manually convert parameters to their intended types in your controller:

let { pageSize, pageNumber, marketregion, category, fromDate, toDate, group_size } = req.body;

// Convert numeric values to integers
pageSize = parseInt(pageSize, 10);
pageNumber = parseInt(pageNumber, 10);
group_size = parseInt(group_size, 10);

// Convert stringified arrays back to actual arrays
try {
  if (typeof marketregion === 'string') {
    marketregion = JSON.parse(marketregion);
  }
} catch (err) {
  // Handle invalid JSON (e.g., set a default array)
  marketregion = [];
}

Just remember to wrap JSON.parse in a try/catch to avoid crashes if the string isn’t valid JSON.

Verify the Fix

Add a quick console log to confirm the types are correct after implementing any solution:

console.log(typeof pageSize); // Should log "number"
console.log(Array.isArray(marketregion)); // Should log "true"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 07:00:27