Sencha Touch - HasOne Setter doesn't seem to work - javascript

So I have something along the lines of:
events: [
{
id: 1,
name: 'Birthday Party',
organiser: {
id: 1,
name: 'Jill'
},
attendees: [
{
id: 2,
name: 'Bob'
},
{
id: 3,
name: 'Jill'
}
]
}
]
As usual: Events have a name, it has attendees but I'm also looking at having one organiser.
So I've setup my HasOne proxy within the Event to HasOne Organiser and I'm trying to create the Organiser object and attach it to the Event within the console but am having no luck.
I tried something like this, as the documentation says there should be a setter created... although only seems to give example for setting an ID (which also doesn't work).
var event = Ext.create('App.model.Event', {id: 1, name: 'Birthday Party'});
event.getOrganiser(); // says undefined
event.setOrganiser({
id: 1,
name: 'Jill'
}); // says TypeError: Object #<Object> has no method 'get'
event.setOrganiser(1); // As the example but it still says the TypeError message
event.setOrganiser(Ext.create('App.model.Organiser', {
id: 1,
name: 'Jill'
}); // says TypeError: Object #<Object> has no method 'get'
So I'm confused... examples from Doc don't seem to be working for me but I need to pass in the rest of the data anyway. No idea how to approach this without spending an entire day going into the source code, dumping and breakpointing everything.
Any ideas? Should I be able to do any of the above or is it not meant to work that way? I want to POST both the Event and the Organiser object with one call... so was expecting JSON like this when I do event.save();
{
id: 1,
name: 'Birthday Party',
organiser: {
id: 1,
name: 'Jill'
}
}
Sorry if you've seen this on the ST forum but it seems to be a ghost town
Thanks for any pointers or help :)

Updated to ST 2.0.1.1 and this is what worked:
event.setOrganiser(Ext.create('App.model.Organiser', {
id: 1,
name: 'Jill'
});

Related

Nodejs | Object property is available but it's returning 'undefined'

I've been trying to solve this for about 2 hours. I am returning an object in Nodejs. This object has 3 properties. I can access the first two, but when I try to access the third property, I get the 'undefined' warning. I'm about to go crazy can you help me please?
Request:
const categories = await Categories.find({}); //mongoose query
console.log(categories[0]); //its returning Object
console.log(categories[0].category); //its returning []
console.log(categories[0]._id); //its returning new ObjectId("6257233b897c3b8785ff625b")
console.log(categories[0].mainCategories); //its returning undefined ?? wtf?!
console.log(categories[0].hasOwnProperty("category")); //its returning false
console.log(categories[0].hasOwnProperty("_id")); //its returning false
console.log(categories[0].hasOwnProperty("mainCategories")); //its returning false
Object Result:
{
category: [],
_id: new ObjectId("6257233b897c3b8785ff625b"),
mainCategories: [
{ id: 1000010100, name: 'Konut', subCategories: [Array] },
{ id: 1000010200, name: 'Devremülk', subCategories: [Array] },
{ id: 1000020100, name: 'İşyeri', subCategories: [Array] },
{ id: 1000020200, name: 'Arazi', subCategories: [Array] },
{
id: 1000020300,
name: 'Turistik İşletme',
subCategories: [Array]
}
]
}
The answers given by Cristian Traìna and VebDav caused lightning in my brain. I couldn't find the exact solution to the problem on the internet. But different problems guided me. The problem stems from my stupidity. I forgot to enter the mainCategories field in the mongoose category schema. I added this and its fixed. Thank you so much everyone for your answers.
And now its returning mainCategories :)

Apollo weirdly alters the query result

I'm using react-apollos Query-Component in React Native to get data from my backend.
The result looks something like this:
[
{
id: 1,
name: 'US Election',
parties: [
{
name: 'democrats',
id: 4,
pivot: {
id: 3,
answers: [
{
id: 13,
question_id: 3,
reason: 'Lorem ipsum',
__typename: "Answer"
},
{
id: 14,
question_id: 5,
reason: 'Lorem ipsum',
__typename: "Answer"
},
],
__typename: "ElectionPartyPivot"
},
__typename: "Party"
},
],
__typename: "Election"
},
{
id: 2,
name: 'Another one',
parties: [
{
name: 'democrats',
id: 4,
pivot: {
id: 7,
answers: [
{
id: 15,
question_id: 7,
reason: 'Lorem ipsum',
__typename: "Answer"
},
{
id: 18,
question_id: 9,
reason: 'Lorem ipsum',
__typename: "Answer"
},
],
__typename: "ElectionPartyPivot"
},
__typename: "Party"
},
],
__typename: "Election"
}
]
Now, when I console.log the result, the second election "Another one" has the pivot from the first entry US Election.
I think this is because of the normalization that goes on within Apollo (Cause the ID of the parties are the same in both) but I'm unsure how to fix it, so that it does not normalize this or normalizes it correctly.
EDIT
I came up with this solution, but it looks hacky. I now get the election_id together with the party and create a different Identifier within the cache. I wonder if this is good practice?
const cache = new InMemoryCache({
dataIdFromObject: object => {
switch (object.__typename) {
case 'Party': return `${object.election_id}:${object.id}`;
default: return defaultDataIdFromObject(object);
}
}
});
const client = new ApolloClient({
uri: config.apiUrl,
cache
});
Yes, providing a custom dataIdFromObject would be necessary in this case. You should consider using Party:${object.election_id}:${object.id} as the key in case there are other Election fields in the future that will require the same treatment.
This is, at the root, an issue with the way the schema is designed. There's an underlying assumption in GraphQL that while the nodes in your GraphQL may have relationships with one another, they are fully independent of each other as well. That is to say, within the same context, the same node should not represent different data based on the presence or absence of other nodes in the response.
Unfortunately, that's exactly how this response is structured -- we have a node that represents a Party, but its fields are different depending on its relationship to another node -- the Election.
There's two ways to remedy this sort of issue. One way would be to maintain different instances of each Party with different ids for each Election. Rather than representing a political party over the course of its life, the underlying data model behind the Party type would present a political party only in the context of one election.
The other way would be to restructure your schema to more accurately represent the relationships between the nodes. For example, a schema that supported this kind of query:
{
elections {
id
name
parties {
id
name
# omit pivot field on Party type
}
# instead because pivots are related directly to elections, add this field
pivots {
id
answers
# because we may still care what party the pivot is associated with as well
# we can add a party field to pivot to show that relationship
party {
id
name
}
}
}
}

Redux: local state id's and/or api uuid's

I'm using Redux with a REST api that uses UUID's. The usual pattern for storing state is using id's as a key for objects:
entities: {
articles: {
1: {
id: 1,
title: 'Some Article',
author: 1
},
2: {
id: 2,
title: 'Other Article',
author: 1
}
},
users: {
1: {
id: 1,
name: 'Dan'
}
}
}
How would I use the UUID's from the api in this? I'd like to be able to create a new entity without having to request the UUID from the server first (for offline capabilities).
Should I:
Use local id's, keep the UUID in a _id property of the entity, and only use it when making an API request? This seems the easiest way, although it feels redundant and I will probably have to search through entities for a certain _id in some cases.
entities: {
articles: {
1: {
_id: 'UUID',
title: 'Some Article',
author: 1
},
2: {
id: 'UUID',
title: 'Other Article',
author: 1
}
},
users: {
1: {
_id: 'UUID',
name: 'Dan'
}
}
}
Use only UUID's from the API, and when creating a new item use a sort if temporary id until the API call is resolved? This seems the best way, although I'm not sure how I would go about changing the id's, which also feels wrong (as they're id's).
entities: {
articles: {
'UUID': {
_id: 'UUID',
title: 'Some Article',
author: 'UUID'
},
'UUID': {
_id: 'UUID',
title: 'Other Article',
author: 'creating'
}
},
users: {
'UUID': {
_id: 'UUID',
name: 'Dan'
},
'creating': {
name: 'Stan'
}
}
}
Do it some other way?
I wouldn't add it to the Redux store until the API returns a response.
In Vue, in my data for a given component I usually have two (sometimes more) root keys, one of which points to the part of my Redux store that handles the data, and the other that is usually form or something of that sort, and the Vue data changes due to binding.
After the user initiates the action to add the resource (POST /resources), and the server returns a successful response, I dispatch an action addedResource. And prior to that I'd dispatch something like addingResource, etc.
Is there any reason this wouldn't work? There shouldn't be a difference using an auto incremented, integer id field vs. a UUID. Your data normalization should still work the same way.

cannot read property of undefined - but property exists

console.log(result.data[0]);
returns:
{ name: 'page_stories',
period: 'day',
values:
[ { value: 114, end_time: '2016-07-31T07:00:00+0000' },
{ value: 68, end_time: '2016-08-01T07:00:00+0000' },
{ value: 134, end_time: '2016-08-02T07:00:00+0000' } ],
title: 'Daily Page Stories',
description: 'Daily: The number of stories created about your Page. (Total Count)',
id: '462582393917692/insights/page_stories/day' }
but for some reason var name = result.data[0].name; is returning:
TypeError: Cannot read property 'name' of undefined
I can't for the life of me figure out why. the 'name' property clearly exists as does the result.data[0] object. Any help would be fantastic.
edit: found out that the data format wasn't uniform for every single request (just most!). Thanks for the responses. I should've made it clear that I was feeding batch requests through this code too.
Ensure that you are actually trying to access an object and not a string. JSON.parse(obj) if needed.

Efficient algorithm / recursive function to nest items from a list

I'm currently implementing my own commenting system. Unfortunately Disqus or any other comment platform doesn't meet my requirements.
I use NodeJS and MongoDB as backend. I need to run basically two queries on my database:
Get all comments by a topic/slug
Get all comments by a user
One can comment to an topic or reply to a comment.
Hey, cool post # top lvl comment
Thanks! # reply to comment
Foo Bar! # reply to reply
and so on...
So my database schema looks like
{
id: ObjectId,
text: string,
author: { id: ObjectId, name: string },
parent: nullable ObjectId,
slug: string/number/whatever
}
If parent is null it's a top level comment, otherwise it's a reply.
Pretty easy so far, right? The problem I do have now is displaying comments below posts. When there would be only top level comments it would be easy. Just get all comments for one specific slug, sort them by date/rating/... and compile them with my HTML View Engine.
But there are in fact replies and I'm just stuck at the point where I need to organize my structure. I want to nest replies into comments within my list
Original list (simplified)
[
{ id: 1, text: 'foo', parent: null },
{ id: 2, text: 'bar', parent: 1 },
// ...
]
Expected Output
[
{ id: 1, text: 'foo', replies: [
{ id: 2, text: 'bar' },
] },
]
I've tried creating my expected output with a recursive function which got very weird tho. Unless that it wouldn't be very efficient. So since I'm really getting frustrated and kinda feeling stupid not solving this problem I've decided to ask for your help SO.
The actual problem I want to solve: How do I render my comments, that they are properly nested etc.
The question I'm going to ask: How do I organize my flat structure in an efficient way to solve the above described problem?
Here's one approach with linear complexity:
var comments = [{
id: 3,
text: 'second',
parent: 1
}, {
id: 1,
text: 'root',
parent: null
}, {
id: 2,
text: 'first',
parent: 1
}, {
id: 5,
text: 'another first',
parent: 4
}, {
id: 4,
text: 'another root',
parent: null
}];
var nodes = {};
//insert artificial root node
nodes[-1] = {
text: 'Fake root',
replies: []
};
//index nodes by their id
comments.forEach(function(item) {
if (item.parent == null) {
item.parent = -1;
}
nodes[item.id] = item;
item.replies = [];
});
//put items into parent replies
comments.forEach(function(item) {
var parent = nodes[item.parent];
parent.replies.push(item);
});
//root node replies are top level comments
console.log(nodes[-1].replies);

Categories

Resources