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

Denormalizing a list doesn't work the same as in the README #22

Closed
krisfields opened this issue Oct 7, 2016 · 6 comments
Closed

Denormalizing a list doesn't work the same as in the README #22

krisfields opened this issue Oct 7, 2016 · 6 comments
Labels

Comments

@krisfields
Copy link

krisfields commented Oct 7, 2016

The README has:

// Denormalize a list
const denormalized = denormalize([article], normalized.entities, articleListSchema);
console.log(denormalized);
// [{
//   id: 1,
//   title: 'Some Article',
//   author: {
//     id: 1,
//     name: 'Dan'
//   },
// }]

But it doesn't actually return an array, and the information isn't nested. Instead it simply gives a key to the author. This is what I get using the code as given in the example in the README:

const denormalized = denormalize([article], normalized.entities, articleListSchema);
console.log(denormalized);
{0: {
       author: 1,
       id: 1,
       title: "Some Article"
}}

I'm not sure how to actually get an array back with the author nested. Any help would be greatly appreciated. Thanks!

@gpbl
Copy link
Owner

gpbl commented Oct 7, 2016

Hi @krisfields, I'm sorry the README is not so clear (something similar is also open in #14).

I can't help you directly right now (I need to reproduce the case first). Maybe you can dig into this test and understand where your code is different?

@krisfields
Copy link
Author

krisfields commented Oct 8, 2016

Is it that it's not clear or that it's inaccurate?

I dug into that test and was not able to see how mine differed at all, other than in the expected result. In fact, I copied/pasted what I believe is the relevant code from that test and still get a different result. Here's what I tried:

import { normalize, Schema, arrayOf } from 'normalizr'

const articleSchema = new Schema('articles')
const userSchema = new Schema('users')
const collectionSchema = new Schema('collections')

articleSchema.define({
  author: userSchema,
  collections: arrayOf(collectionSchema),
})

collectionSchema.define({
  curator: userSchema,
})

const article1 = {
      id: 1,
      title: 'Some Article',
      author: {
        id: 1,
        name: 'Dan',
      },
      collections: [{
        id: 1,
        name: 'Dan',
      }, {
        id: 2,
        name: 'Giampaolo',
      }],
    };
    const article2 = {
      id: 2,
      title: 'Other Article',
      author: {
        id: 1,
        name: 'Dan',
      },
    };
    const article3 = {
      id: 3,
      title: 'Without author',
      author: null,
    };

    const article4 = {
      id: 4,
      title: 'Some Article',
      author: {
        id: '',
        name: 'Deleted',
      },
      collections: [{
        id: '',
        name: 'Deleted',
      }],
    };

const response = {
  articles: [article1, article2, article3, article4],
}

const data = normalize(response, {
  articles: arrayOf(articleSchema),
})

const articles = [
  data.entities.articles['1'],
  data.entities.articles['2'],
]

const denormalized = denormalize(articles, data.entities, arrayOf(articleSchema))
console.log("articles denormalized = ", denormalized)

And the output was not an Array and was not nested, but instead simply had the keys for each object that should have been nested, like before when copying from the README.

articles denormalized = Object {0: Object, 1: Object}
{0: {
  author: 1,
  collections: [{0:1}, {1:2}],
  id: 1,
  title: "Some Article"
},
 1: {
  author: 1,
  id: 2,
  title: "Other Article"
}}

So I'm really curious and confused as to where I may have went wrong.

@gpbl
Copy link
Owner

gpbl commented Oct 8, 2016

@krisfields I'm curious as well.

Could you please check you are using the last version of denormalizr?

npm ls denormalizr

I've setup a small repo to test this case: https://github.com/gpbl/denormalizr-test

git clone https://github.com/gpbl/denormalizr-test.git
cd denormalizr-test
npm install
npm start

it logs:

Normalized data
{ entities: 
   { articles: 
      { '1': { id: 1, title: 'Some Article', author: 1, collections: [ 1, 2 ] },
        '2': { id: 2, title: 'Other Article', author: 1 } },
     users: { '1': { id: 1, name: 'Dan' } },
     collections: 
      { '1': { id: 1, name: 'Dan' },
        '2': { id: 2, name: 'Giampaolo' } } },
  result: { articles: [ 1, 2 ] } }
Denormalized:
[ { id: 1,
    title: 'Some Article',
    author: { id: 1, name: 'Dan' },
    collections: [ { id: 1, name: 'Dan' }, { id: 2, name: 'Giampaolo' } ] },
  { id: 2, title: 'Other Article', author: { id: 1, name: 'Dan' } } ]

So it seems all correct to me (macOS, node v4.4.5).

@krisfields
Copy link
Author

Well, it works correctly for me when I run your test repo, but when I copy/paste the code from that repo's index.js file into my project, it no longer works correctly. The output is no longer an array, and no longer nested. I currently have no idea why but haven't had a lot of time to dig in yet. If you have any suggestions on what I might look into to determine what's going on, any help would be much appreciated.

mac, node v6.6.0

@krisfields
Copy link
Author

Ok, I guess I wasn't using the latest version of denormalizr. "npm upgrade normalizr --save" didn't actually upgrade it, which is what confused me. Now that I am using the latest version, everything works properly.

@gpbl
Copy link
Owner

gpbl commented Oct 9, 2016

Awesome! Yes npm update won't work for minor release < 1.0. I use npm-check for updating dependencies. It is safer :)

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

No branches or pull requests

2 participants