Sails.js ORM create方法报E_INVALID_RECORD错误求助
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 rawDateobject. - 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

