Skip to content

Strange behaviour of the method 'save' within the helper #713

@vmusulainen

Description

@vmusulainen

I have faced with strange behaviour of 'save' method.
I have a class Unit that is extended on server side

Unit.extend({
    meteorMethods: {
        insert() {
            this.name = 'insert name';
            const id = this.save();
            const parentUnit = Unit.findOne({_id: this.parentUnitId});
            console.log('parentUnit', parentUnit);
            parentUnit.changeName();
        },
    },
    methods: {
        changeName(){
            console.log('change name is called');
            this.name = 'changed name';
            console.log(this);
            this.save()
        }
    }
});

When I call method 'insert' from client side like this:

const unit = new Unit({
                name: 'New Unit Name',
                parentUnitId: this.selectedUnitHolder.get()._id,
            });

unit.callMethod('insert');

I get properly stored new unit in db, but parent unit is not updated although console output from changeName method look as

Class {
	_isNew: false,
	_id: 'jiM8ZXBuayz6H4Za5',
	name: 'changed name',
	parentUnitId: undefined}

The field 'name' of parent unit has non-changed value in db.

But! the saving in method work as expected if I call it for the original object like this

Unit.extend({
   meteorMethods: {
       insert() {
           this.name = 'insert name';
           const id = this.save();
           //const parentUnit = Unit.findOne({_id: this.parentUnitId});
           //console.log('parentUnit', parentUnit);
           this.changeName();
       },
   },
   methods: {
       changeName(){
           console.log('change name is called');
           this.name = 'changed name';
           console.log(this);
           this.save()
       }
   }
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions