-
Notifications
You must be signed in to change notification settings - Fork 24
Denormalizing a list doesn't work the same as in the README #22
Comments
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? |
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. |
@krisfields I'm curious as well. Could you please check you are using the last version of denormalizr?
I've setup a small repo to test this case: https://github.com/gpbl/denormalizr-test
it logs:
So it seems all correct to me (macOS, node v4.4.5). |
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 |
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. |
Awesome! Yes |
The README has:
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:
I'm not sure how to actually get an array back with the author nested. Any help would be greatly appreciated. Thanks!
The text was updated successfully, but these errors were encountered: