totaljs / framework

Node.js framework
http://www.totaljs.com
Other
4.36k stars 450 forks source link

Why is my data becoming undefined? #665

Closed sabsaxo closed 6 years ago

sabsaxo commented 6 years ago

I'm passing data to the server through AJAX, but when I try to access my data in the model (a Schema) using the $clean method, my 'data' becomes undefined. Why is that, and HOW do I prevent this from happening?

This is the data I pass from the client:

{
    "id": "6190fb86-e037-43c4-97c0-62ec8b6c43e6",
    "name": "My Second Page",
    "data": {
        "meta": {
            "name": "My Second Page",
            "slug": "my-second-page",
            "type": "page"
        },
        "fields": [
            {
                "id": "a44b5e8b-1fdb-4560-8119-88a4b229a9e3",
                "type": "string",
                "label": "Page Title",
                "value": "My Second Page Title"
            },
            {
                "id": "ed1ad010-4362-4574-b7b5-16dc0a309817",
                "url": "/media/images/page-image-two.jpg",
                "type": "image",
                "label": "My Image",
                "settings": {
                    "size": {
                        "width": 756,
                        "height": 480
                    }
                }
            },
            {
                "id": "8c5f61d1-326d-4ddf-bbe2-f8c222f74af1",
                "type": "article",
                "label": "My First Article for Second page",
                "api_url": "/articles/8c5f61d1-326d-4ddf-bbe2-f8c222f74af1",
                "content": {
                    "text": "<p>Lorem ipsum dolor sit amet. Adding more text for testing.</p>",
                    "title": "Lorem"
                }
            }
        ]
    }
}

This is what I do in my controller:

function save () {
    let _this = this;

    let options = {};
    options.contenttype = arguments[0];
    options.id = arguments[1];
    options.data = this.body.$clean();

    var query_result = _this.$update( options, this.callback() );
}

I've defined the fields in the Schema like this (tried both with and without the quotes around the data types):

...
    schema.define("id", "String");
    schema.define("name", "String");
    schema.define("data", "JSON");
...

And this is what I get in the controller:

// console.log from the controller with a Schema:
{ id: '6190fb86-e037-43c4-97c0-62ec8b6c43e6',
  name: 'My Second Page',
  data: undefined }

Notice how 'data' is undefined, but is clearly in the passed in data.

It seems like the framework cannot properly handle nested JSON objects!?

petersirka commented 6 years ago

Because data is not JSON --> String !!!! It's Object.

sabsaxo commented 6 years ago

Now, even that comment I don't understand. What is object?

sabsaxo commented 6 years ago

What do you mean by JSON --> String?

petersirka commented 6 years ago

object is object. JSON is meant as a string (because JSON is the string) but the framework performs a simple JSON validation. Change json to object and everything will work as you expect.

sabsaxo commented 6 years ago

So, what you're telling me is: that I should CHANGE 'JSON' to 'OBJECT'? JSON --> String didn't make any sense.

sabsaxo commented 6 years ago

According to the 'docs' JSON is defined as "JSON", so the 'docs' may be confusing ...

petersirka commented 6 years ago

OK, otherwise: WHAT IS JSON?

sabsaxo commented 6 years ago

An object. So why can I define something as 'JSON' when I should be using 'OBJECT'? It's confusing at best.

petersirka commented 6 years ago

No, JSON is not OBJECT, it's String!

image

sabsaxo commented 6 years ago

True, but I was expecting the data to be an object when passed to the model on the server – and behaving as such. I think I'm simly confused about what's going on 'behind the scenes (Schemes ; ) )'

But thanks for clearing that out ...