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

Sails.js ORM create方法报E_INVALID_RECORD错误求助

Troubleshooting E_INVALID_RECORD Error in Sails.js Reservation Creation

Hey there, let's tackle that frustrating E_INVALID_RECORD error you're facing when creating reservations with sails-disk. This error almost always stems from a mismatch between the data you're trying to save and the validation rules defined in your Reservation model. Let's break down the most likely fixes step by step:

1. Ensure All Required Fields Are Provided

Your model marks person, date, slot, and partner as required: true. Double-check that your create call includes every single one of these fields—even a missing partner or empty person string will trigger this error.

2. Validate Data Types Match Model Definitions

Each field has strict type requirements, so make sure your input aligns:

  • date: Defined as type: 'number', so you must pass a numeric timestamp (e.g., Date.now()) instead of a date string like "2024-05-20" or a raw Date object.
  • partner: Defined as model: 'user', which means you need to pass the ID of an existing User record (usually a string or number, depending on your User model's primary key) — not the full User object or an invalid ID value.

3. Inspect Full Error Details for Precision

Sails doesn't just throw a generic error—it includes specific details about which fields failed validation. Update your create code to log the full error object, especially the invalidAttributes property:

try {
  const newReservation = await Reservation.create({
    // Your reservation data here
  }).fetch();
} catch (err) {
  console.error('Full error breakdown:', err);
  console.error('Invalid fields:', err.invalidAttributes); // This tells you exactly what's wrong
}

This will point you directly to the problematic field(s) instead of guessing.

4. Verify Your sails-disk Configuration

While sails-disk works out of the box for most cases, confirm your config/datastores.js is set up correctly with no conflicting rules:

module.exports.datastores = {
  default: {
    adapter: 'sails-disk'
  }
};

Example of a Valid Create Call

Assuming you have a User record with ID "user_123", here's how a valid reservation creation should look:

const newReservation = await Reservation.create({
  person: 'Jane Smith',
  date: Date.now(), // Numeric timestamp
  slot: 'afternoon',
  partner: 'user_123' // Existing User ID
}).fetch();

More often than not, the issue is a missing field or incorrect data type—logging the full error details will get you to the fix fastest.

内容的提问来源于stack exchange,提问作者relentless-coder

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:43:52