Simoyw Simoyw -4 years ago 132
Javascript Question

Sails.js: save() model doesn't seems to work Error (E_UNKNOWN)

My model definition looks like this:

//api/models/Table.js
module.exports = {
tableName: 'table',
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey: true,
size: 11
},
title1: {
type: 'string',
index: true,
size: 255
},
title2: {
type: 'string',
size: 255
},
title2: {
type: 'string',
size: 255
},
countries: { //Countries nationalities
model: 'hg_laender'
},
year: {
type: 'string',
size: 4
},
relased: { //released
type: 'string',
enum: ['true', 'false']
},
createdOn: { //Created on
type: 'datetime'
},
changedOn: { // Changed on
type: 'datetime'
},
documents: { //Document type
collection: 'documents',
via: 'table',
dominant: true
},
creator: { //Creator
collection: 'creator',
via: 'table',
dominant: true
},
kanton: { // Canton
collection: 'kanton',
via: 'table',
dominant: true
},
language: {
collection: 'language',
via: 'table',
dominant: true
}
}
};


The code generating the error is the following

Table.find()
.exec((err, tables) => {
if(err) {
sails.log.error('first error', err);
}
sails.log.debug(tables[0]); //The record is loaded correctly
tables[0].save((err, model) => {
if(err) {
sails.log.error('second error', err);
}
});
});


I get the following error message:

error: second error Error (E_UNKNOWN) :: Encountered an unexpected error
at new WLError (/home/xxx/node_modules/waterline/lib/waterline/error/WLError.js:25:15)
at /home/xxx/node_modules/waterline/lib/waterline/model/lib/defaultMethods/save.js:196:17
at /home/xxx/node_modules/async/lib/async.js:52:16
at /home/xxx/node_modules/async/lib/async.js:550:17
at /home/xxx/node_modules/async/lib/async.js:544:17
at _arrayEach (/home/xxx/node_modules/async/lib/async.js:85:13)
at Immediate.taskComplete (/home/xxx/node_modules/async/lib/async.js:543:13)
at processImmediate [as _immediateCallback] (timers.js:383:17)


If a use the
create()
method it works. t actually save the record on the database. But if I try to update it, it doesn't work.

Any idea of what I'm doing wrong?

Additional info:
- sails.js version: 0.12.8

Answer Source

Seems like there was a problem if a manually specify the id of the model. Now i changed the model like below and it works. Btw I don't know why specifing the id will not work. Also it' doesn't throw any error when creating a model..

//api/models/Table.js
module.exports = {
    tableName: 'table',
    autoCreatedAt: false,
    autoUpdatedAt: false,
    attributes: {
        //id: {
        //    type: 'integer',
        //    autoIncrement: true,
        //    primaryKey: true,
        //    size: 11
        //},
        title1: {
            type: 'string',
            index: true,
            size: 255
        },
        title2: {
            type: 'string',
            size: 255
        },
        title2: {
            type: 'string',
            size: 255
        },
        countries: { //Countries nationalities
            model: 'hg_laender'
        },
        year: {
            type: 'string',
            size: 4
        },
        relased: { //released
            type: 'string',
            enum: ['true', 'false']
        },
        createdOn: { //Created on
            type: 'datetime'
        },
        changedOn: { // Changed on
            type: 'datetime'
        },
        documents: { //Document type
            collection: 'documents',
            via: 'table',
            dominant: true
        },
        creator: { //Creator
            collection: 'creator',
            via: 'table',
            dominant: true
        },
        kanton: { // Canton
            collection: 'kanton',
            via: 'table',
            dominant: true
        },
        language: {
            collection: 'language',
            via: 'table',
            dominant: true
        }
    }
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download