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

Node.js 9.8.0(Linux)中字符串下划线自动转空格问题求助

Fixing Underscore-to-Space Conversion in Node.js 9.8.0 for PostgreSQL Queries

Hey Allen, that’s a weird and frustrating issue—underscores turning into spaces in strings and JSON keys is definitely not default Node.js behavior, so let’s track down the root cause and fix it.

Possible Causes & Solutions

1. Accidental String Replacement Code

First, scan your codebase for unintended string replacement logic targeting underscores. It’s easy to accidentally add a replace call that affects more data than you intended. Look for lines like:

// This would replace all underscores with spaces across strings/object keys
const transformed = str.replace(/_/g, ' ');

Check utility functions, middleware, or data processing pipelines that touch your data before it reaches PostgreSQL. Search for regex patterns like /_/g that might be modifying keys without your knowledge.

2. Misconfigured Third-Party Libraries

If you’re using an ORM (like Sequelize), JSON parsers, or API formatters, these tools often have settings for transforming key names (e.g., snake_case to camelCase). A misconfiguration could be causing them to convert underscores to spaces instead.

For example, double-check custom JSON.parse reviver functions—make sure they aren’t altering keys:

// Bad reviver that replaces underscores with spaces
const data = JSON.parse(rawJson, (key, value) => {
  if (typeof key === 'string') {
    return key.replace(/_/g, ' '); // This is the culprit!
  }
  return value;
});

If using an ORM, verify settings like underscored or keyTransform aren’t set to an unexpected value.

3. Debug the Data Pipeline

Pinpoint exactly where the transformation happens by adding console logs at each stage of your data flow:

// Log raw input data
const rawData = await getYourData(); // e.g., from API, file, etc.
console.log('Raw keys:', Object.keys(rawData)); // Should show first_name, last_name

// Log after processing steps
const processedData = processData(rawData);
console.log('Processed keys:', Object.keys(processedData));

// Log right before querying PostgreSQL
console.log('Keys before query:', Object.keys(processedData));

This will tell you whether the issue occurs when fetching data, processing it, or passing it to your database client.

Workaround for PostgreSQL Queries

While you fix the root cause, you can work around the issue by explicitly using snake_case column names in your queries, even if your object keys have spaces:

// Hardcode the column name and use the space-separated key to get the value
const firstName = user['first name'];
db.query('SELECT * FROM users WHERE first_name = $1', [firstName]);

Long-Term Note

Node.js 9.8.0 is an outdated, end-of-life version (EOL since June 2018). Upgrading to a supported LTS version (like 18.x or 20.x) could resolve obscure bugs contributing to this issue, plus you’ll get security patches and new features.


内容的提问来源于stack exchange,提问作者Allen Tzul

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:17:05