Skip to content

hasOne delAccessor not working #226

Closed

Description

@ctaepper

i define a status for a person:

Person.hasOne('status', Status, {
    autoFetch: true
});

i can set and change that status, but i cannot remove it:

person.removeStatus(callback);

i add beforeSave and afterSave hooks to the Person Model:

hooks: {
    beforeSave: function(next) {
        console.log('beforeStatus', this.status_id);
        next();
    },
    afterSave: function() {
        console.log('afterStatus', this.status_id);
    }
}

it seems like the hooks are called 3 times (???)
beforeSave the first time tells me the expected value (this.status_id = null)
but before afterSave is called, beforeSave is called another 2 times having the original value of status_id... after those 3 beforeSave calls, the 3 afterSave calls are called, each of them having the original value of status_id in it, and not my expected value null

help?

Activity

  1. dresende commented on Jul 16, 2013

    @dresende
    Owner

    I'm going to take a closer look.

  2. dresende commented on Jul 16, 2013

    @dresende
    Owner

    I actually see 2 beforeSave calls and 2 afterSave calls. Are you sure it's 3 beforeSave ? Can you show me your Person model ?

  3. dresende commented on Jul 16, 2013

    @dresende
    Owner

    In my test file, there are 2 calls because somehow the Model is making an UPDATE query to set status_id to NULL and then making a new UPDATE query to set status_id to the previous one..

  4. ctaepper commented on Jul 16, 2013

    @ctaepper
    Author
    var _s = require('underscore.string');
    var _ = require('underscore');
    var ImageResource = require('./imageresource');
    var Status = require('./status');
    var async = require('async');
    
    var Person = null;
    
    module.exports = {
    
        init: function (db, cb) {
            Person = db.define("person", {
                title: String,
                firstname: String,
                lastname: String,
                job: String,
                phone: String,
                email: String,
                sort: Number,
    
                created_at: {type: "date", time: true},
                edited_at: {type: "date", time: true}
            });
    
            Person.hasOne('imageresource', ImageResource.model(), {
                autoFetch: true
            }).hasOne('status', Status.model(), {
                autoFetch: true
            });
            cb();
        },
    
    
        create: function (data, cb) {
            Person.create({
                title: data.title,
                firstname: data.firstname,
                lastname: data.lastname,
                job: data.job,
                phone: data.phone,
                email: data.email,
                sort: 999,
                status_id: 1,
                created_at: new Date()
            }, cb)
        },
    
        setImageResources: function (id, resource, cb) {
            Person.get(id, function (err, person) {
                if (err) return cb(err);
    
                if (resource && resource.length) {
                    person.setImageresource(_.first(resource), cb);
                } else {
                    // TODO check github issues for error fix
                    person.removeImageresource(cb);
                }
            });
        },
    
        setStatus: function (id, status, cb) {
            Person.get(id, function (err, person) {
                if (err) return cb(err);
                person.setStatus(status, cb);
            })
        },
    
        addImageResources: this.setImageResources,
    
        get: function (id, cb) {
            Person.get(id, cb);
        },
    
    
        find: function (options, cb) {
            Person.find(options, cb);
        },
    
    
        update: function (id, data, cb) {
            Person.get(id, function (err, obj) {
                if (err) return cb(err);
    
                delete data['id'];
                delete data['created_at'];
    
                data.edited_at = new Date();
    
    
                obj.save(data, cb);
            });
        },
    
        remove: function (id, cb) {
            Person.get(id, function (err, obj) {
                if (err) return cb(err);
                obj.remove(cb);
            });
        },
    
        model: function () {
            return Person;
        }
    };
    
    
    
  5. ctaepper commented on Jul 16, 2013

    @ctaepper
    Author

    yes, i am 100% sure its 3 calls... in this file it is actually the imageresource i want to remove... but it also works with status

  6. dresende commented on Jul 16, 2013

    @dresende
    Owner

    I think the hooks are being called 3 times because of the 2 associations. I only have one so it's called 2 times. I'm going to test with more associations.

  7. dresende commented on Jul 16, 2013

    @dresende
    Owner

    This was nasty to debug, simple to fix :)

    In the meantime, if you want to use the git version, do:

    npm install dresende/node-orm2
    
  8. ctaepper commented on Jul 16, 2013

    @ctaepper
    Author

    i debugged myself for 2 hours before i gave up and left it to you ;-)
    but thanks, will check it out tonight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions