balderdashy / sails

Realtime MVC Framework for Node.js
https://sailsjs.com
MIT License
22.84k stars 1.95k forks source link

Model not destroyed #6289

Closed Thomasdezeeuw closed 5 years ago

Thomasdezeeuw commented 11 years ago

Another error accorded when I was testing using this code:

        it('should be able find by login and destroy the user', function (done) {
            User.findOneByLogin('MyLogin3', function (err, user) {
                if (err) { return done(err); }

                if (!user) { return done(new Error('User not found')); }

                user.destroy(function (err) {
                    if (err) { return done(err); }

                    User.findOneByLogin('MyLogin3', function (err, user) {
                        if (err) { return done(err); }

                        if (!user) { return done(); }

                        done(new Error('User not destroyed'));
                    });
                });
            });
        });
module.exports = {

    tableName: 'users',
    autoPK: false,

    attributes: {
        id: {
            type: 'INTEGER',
            required: true,
            primaryKey: true,
            maxLength: 255
        },
        login: {
            type: 'STRING',
            alphanumeric: true, // future, alphadashed? https://github.com/chriso/node-validator/issues/233
            required: true,     // Or: https://github.com/balderdashy/anchor/pull/27
            unique: true,
            maxLength: 255
        },
        name: {
            type: 'STRING',
            required: true,
            maxLength: 255
        },
        website: {
            type: 'STRING',
            maxLength: 255
        },
        avatar: {
            type: 'STRING',
            maxLength: 255
        },
        email: {
            type: 'EMAIL',
            maxLength: 255
        }

        // Added by sails:
        // Created_at & Updated_at
    },

    beforeCreate: function (values, done) {
        var cbCount = 0;
        var errCb = false;

        var callback = function (err, user) {
            if (errCb) { return; }

            if (err) {
                errCb = true;
                return done(err);
            }

            if (user) {
                errCb = true;
                done(new Error("User already exists"));
            }

            cbCount++;

            if (cbCount === 2) { return done(); }
        };

        User.findOne(Number(values.id), callback);

        User.findOneByLogin(values.login, callback);
    },

    beforeUpdate: function (values, done) {
        // Make sure that id can't be updated
        if (values.id) {
            delete values.id;
        }

        // Make sure that login can't be updated
        if (values.login) {
            delete values.login;
        }

        done();
    },

    beforeDestroy: function (values, done) {
        if (values.where === null) {
            done(new Error("Can't delete all users"));
        } else {
            done();
        }
    },
};

It doesn't destroy the user, Error:

Error: User not destroyed at /Users/thomas/Dropbox/www/WebBlueprint/Api/test/unit/models/User.test.js:334:12 at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails/node_modules/waterline/lib/waterline/query/finders/basic.js:78:7 at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails/node_modules/waterline/lib/waterline/adapter/dql.js:37:7 at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/lib/adapter.js:455:23 at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/lib/adapter.js:311:15 at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:158:16 at commandHandler (/Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/cursor.js:651:16) at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/db.js:1670:9 at Server.Base._callHandler (/Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/base.js:382:41) at /Users/thomas/Dropbox/www/WebBlueprint/Api/node_modules/sails-mongo/node_modules/mongodb/lib/mongodb/connection/server.js:472:18

vanetix commented 11 years ago

What does your User collection definition look like?

Thomasdezeeuw commented 11 years ago

Updated my first post

particlebanana commented 10 years ago

Just tested this on the the codebase in master and it works. Closing this for now ping me here if this is still and issue and I will re-open it.