Skip to content

Autovalue not working when updating in nested object - simple example included #277

@naschpitz

Description

@naschpitz

Hello there,

After updating SimpleSchema and Collection2 to the newest version (3.0.0), autovalue in nested objects started to behave differently. At first the "defaultValue: {}" was missing (https://github.com/aldeed/simple-schema-js/blob/master/CHANGELOG.md#100) and it solved the issue that autoValue was not being called at all, no matter upon insert or update.

Now I realized that upon updates, despite the fact autoValue is being called, the value returned is not persisted.

Test scenario:

const TestSchema = new TestSchema({
   test: {
       type: Date,
       label: "Updated At",
       optional: true,
       denyInsert: true,
       autoValue: function () {
           if (this.isUpdate || this.isUpsert)
           {
               console.log("test run");
               const date = new Date();
               console.log("value: " + date);
               return date;
           }
       }
   },
   dates: {
       type: DatesSchema,
       label: "Dates",
       optional: true,
       defaultValue: {}
   }
});
const DatesSchema = new SimpleSchema({
    createdAt: {
        type: Date,
        label: "Created At",
        optional: true,
        denyUpdate: true,
        autoValue: function () {
            if (this.isInsert) {
                console.log("createdAt run");
                const date = new Date();
                console.log("value: " + date);
                return date;
            }

            else if (this.isUpsert) {
                return {$setOnInsert: new Date()};
            }

            else
                this.unset();  // Prevent user from supplying their own value
        }
    },
    updatedAt: {
        type: Date,
        label: "Updated At",
        optional: true,
        denyInsert: true,
        autoValue: function () {
            if (this.isUpdate || this.isUpsert)
            {
                console.log("updatedAt run");
                const date = new Date();
                console.log("value: " + date);
                return date;
            }
        }
    },
});

Inserting:
If I insert a new document to the collection which TestSchema is attached to, log will print:

I20180613-13:35:27.027(-3)? createdAt run
I20180613-13:35:27.028(-3)? value: Wed Jun 13 2018 13:35:27 GMT-0300 (-03)

Taking a look at the collection, 'dates.createdAt' IS present as expected.

Updating
If I update a document to the collection which TestSchema is attached to, log will print:

I20180613-13:37:38.710(-3)? test run
I20180613-13:37:38.710(-3)? value: Wed Jun 13 2018 13:37:38 GMT-0300 (-03)
I20180613-13:37:38.711(-3)? updatedAt run
I20180613-13:37:38.711(-3)? value: Wed Jun 13 2018 13:37:38 GMT-0300 (-03)

Taking a look at the collection, 'test' IS present as expected, but 'dates.updatedAt' IS NOT PRESENT.

What am I missing here? Is this the expected behavior? I mean, both "test" and "updatedAt" are the same, the only difference is that one is nested and the other is not.

Being completely honest, I'm not certain if this is a collection2 issue or a simpleschema issue. Autovalue seems to be running as expected in both scenarios, insert and update, but the value is not persisted when on nested objects.

Thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions