Skip to content
This repository has been archived by the owner on Nov 17, 2017. It is now read-only.

Denormalised child objects are always empty #24

Closed
mhaddon opened this issue Oct 22, 2016 · 2 comments
Closed

Denormalised child objects are always empty #24

mhaddon opened this issue Oct 22, 2016 · 2 comments

Comments

@mhaddon
Copy link

mhaddon commented Oct 22, 2016

I copied the code from the example:


    const { normalize, Schema, arrayOf } = require("normalizr/dist/normalizr.min.js");
    const { denormalize } = require("denormalizr");

    const articleSchema = new Schema('articles');
    const authorSchema = new Schema('author');
    const articleList = arrayOf(articleSchema);

    articleSchema.define({
        author: authorSchema,
    });

    authorSchema.define({
        articles: articleList,
    });

    const JSONResponse = {
        "articles": [{
            "id": 2,
            "title": "You won\'t believe what this high order component is doing",
            "author": {
                "id": 1,
                "name": 'Dan',
                "articles": [2],
            },
        }],
    };

    const normalized = normalize(JSONResponse, {
        articles: articleList,
    });

    const article = normalized.entities.articles['2'];
    const denormalized = denormalize(article, normalized.entities, articleSchema);

    console.log(denormalized.author.articles[0] === denormalized); // true

The denormalized variable returns:

author: Object
id: 2
title: "You won't believe what this high order component is doing"

Which is great... but author is an empty object.

I have also had this problem when trying to implement this code with other normalised objects, I have tried using previous versions of the denormalizr too, but that too doesnt work. I am using normalizr version 2.2.1 on Chromium web browser.

@mhaddon
Copy link
Author

mhaddon commented Oct 23, 2016

The problem seems to be that in the denormalize function, when an individual entity is being parsed, it does not enter this if statement "if (schema instanceof _EntitySchema2.default)" (line 172).

This causes line 180, which clones the object, to instead clone it to an empty object.

As a temporary fix if i change line 172 to: "if (schema instanceof _EntitySchema2.default || typeof obj === "number") {", the denormalizr works.

I dont know why the schema wont be seen as a child of EntitySchema2, at the moment, if i have more time i might try to make the temporary fix into something more... substantial.

@gpbl
Copy link
Owner

gpbl commented Feb 11, 2017

The last version is using the latest normalizr and has some changes that may solve this issue – could you please upgrade and try? Thanks

@gpbl gpbl closed this as completed Feb 12, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants