Friday 14 April 2017

Sails + Waterline : undefined after model create()

I'm new to Sails. I created a basic model for an object 'Note', in api/models/Note.js

module.exports = {

    attributes: {

        text: {
            type: 'string',
            defaultsTo: 'Default text for a note',
        },

    },
};

I also have a very basic function that creates a 'Note' object:

createNote(text, callback) {
    Note.create({
            text: text,
        }
    ).exec(function (err, note) {
        sails.log('note created: ' + note);
        if (err) {
            throw err;
        }
        else {
            callback(note);
        }
    });
}

I then use this function to create a new note :

createNote('some text for my Note', (note => {
    sails.log('note created : ' + note.text);
}));

The object Note is successfully created in my database (mongoDb, using sails-mongo@1.0.0-7). But the problem is that in the function createNote(), the note object returned in the exec() callback is undefined.

So in the end, I have this error:

TypeError: Cannot read property 'text' of undefined

comming from this particular line:

sails.log('note created : ' + note.text);

because the note object returned by createNote() is undefined...

What am I doing wrong ?



via John Kerman

No comments:

Post a Comment