Syntax to access json array - javascript

I don't know the syntax to access the "relatedTo" array of my Json object.
When I do JSON.parse(data).projects[0].name I have "New Pretender",
when I do JSON.parse(data).projects[0].relatedTo[0] I have undefined
I have tried few different syntaxes but none of them was working...
The structure is like this :
{
"projects": [{
"name":"New Pretender",
"shortDescription":"A love coach not like the others",
"year":"2016",
"description":"An interactive game/fiction design object you can play with a boob-shaped joystick. Talk about manipulation and other things.",
"relatedTo": ["ecriture", "rhetorique", "jeu"]
}, {
"name":"Pénombre",
"shortDescription":"A game about go out",
"year":"2016",
"description":"This game invites th player to experiment what it is to be alone in a huge world, not knowing where you have to go.",
"relatedTo": ["jeu", "carte"]
}, {
"name":"Bodmer Lab",
"shortDescription":"A generative book based on Martin Bodmer' Faust collection",
"year":"2016",
"description":"This is a book made with a lots of digitized old books that are in the Martin Bodmer huge collection.",
"relatedTo": ["ecriture", "generatif"]
}]
}

Given the structure above, projects[0].relatedTo[0] would return ecriture. I do not see anything incorrect with your syntax. However, it seems as if you may not be looking at the object you think you are.
Is it possible projects[0] is pointing to a project that does not have any relations (i.e. an empty list)? This would result in undefined when trying to access relatedTo[0]. I'd encourage you to check out what relatedTo is, and even what projects[0] is.

I feel so idiotic... I forgot that I have duplicated my json file so I was just calling a file in which the property doesn't exist. I changed the path and now it's ok.

Related

Javascript local scopes and using objects - best practice?

I am currently working on large projects that make use of lots of javascript files.
I then start learning of using local scopes and using objects.
What I do not really understand is how to call them into you local scope?
E.g if I create an object in an local scope in file-a, how can I use them as in a function in the document.ready scope file-b?
I get that you can find this online, but I get demotivation by the high amount of javascript on the internet and can't really find good examples or material. Any help?
Not sure,but I think you might be referring to the use of namespaces within JavaScript as a way to avoid adding all your functions to the main window object.
The Ugly Way
Let's assume you have 3 functions related to cats:
Function AddCat(cat) {
}
Function DeleteCat(catId) {
}
Function BreedCat(cat,cat) {
}
The way these items are coded, they are globally available. Not only does that clutter up your window object, it's hard to share data between these functions in a discreet way.
As long as this js file is loaded, any function in your app can call these functions just by calling AddCat()
Cleaner
To solve that problem, we could create a Cats object that acts as a "namespace" here:
Cats = {
AddCat: function(cat) {
},
DeleteCat: function(catId) {
},
BreedCat: function(cat,cat) {
}
}
Now, you've only added ONE object to the windows class: Cats. In addition, other methods in your web app can call any of those 3 items by calling Cats.AddCat() for example.
This lets you encapsulate all of the Cat data in your entire system within a single "namespace, so it's easier to read.
This can get a lot more detailed. By encapsulating items like this, you can start to hide variables that all your cat routines require from the rest of your code.
There is an excellent set of resources on this type of namespacing (including tons of detail) here and here with links that lead you deeper.
Is that what you were looking for?

pre-processing data into a hard-coded array

I have a question about hard-coded arrays. I looked at several previously posed questions about hard-coded arrays in hopes of getting my answer that way. But, for the most part, I don't understand the answers, and this is the only one that seems like it might be relevant:
glob() to build array of files, or hardcode array? Speed is key, but automation is nice
My question is a lot simpler, though. I have several worksheets in an OpenOffice spreadsheet which I have chosen to pre-process into a large hard-coded array which I will then store inside my 'server' dir. In order to test this, I put the following lines of code into a file called 'distances.js' and placed that file in a 'server' folder directly inside my app directory:
var distances = {};
distances['Salt Lake City.Washington, DC'] = 2080;
distances['Salt Lake City.Cheyenne'] = 434;
distances['Salt Lake City.Denver'] = 536;
distances['Salt Lake City.Carson City'] = 534;
Then I ran the following command in my console to see if I'd be able to access these array values in my app:
console.log(distances['Salt Lake City.Carson City']);
The result I got was:
Uncaught ReferenceError: scores is not defined(…)
I then attemped to insert those lines inside the regular project.js file inside the Meteor.startup function inside of Meteor.isServer:
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
var distances = {};
distances['Salt Lake City.Washington, DC'] = 2080;
distances['Salt Lake City.Cheyenne'] = 434;
distances['Salt Lake City.Denver'] = 536;
distances['Salt Lake City.Carson City'] = 534;
});
}
This resulted in the same error.
I have the 'insecure' package installed in my project, so security shouldn't be an issue. I think I'm just missing something fundamental about where code needs to go in order to be seen by the compiler/interpreter. Can anyone help?
I'm sort of half expecting someone to suggest that I put all of this information into a collection. I don't currently understand why it would be advantageous to do so, but maybe I'm missing something fundamental about the usefulness of doing it this way. If so, could someone explain or point me to a place where I can read about this for myself? I have worked through a couple of meteor tutorials, most recently Your Second Meteor Application. And these are excellent tutorials from which I've learned a lot. But I feel like there are still holes in my knowledge which need to be addressed, this being a prime example.
My plan is to access these hard-coded array elements through a function call which looks something like this:
getDistance('Salt Lake City','Cheyenne')
Because I don't store backwards values, eg. the distance from Cheyenne to Salt Lake City, I intend to set up the function so that, if a specific reference is undefined, it will turn the two elements around and call the function again the same way but with those inverted values (ie. getDistance('right','left') in place of getDistance('left','right')).
But, currently, I can't even get past step one.
Thanks in advance for any assistance you can provide me with.
The answer would seem to be to use the fs module to read data out of your textfile and into your collection. I'm still working on getting that going, but there's more info here: Using nodejs fs module within my meteor app

TypeError: Converting circular structure to JSON - find error in json

I'm using Contentful with a MEAN stack. I query the Contentful API and get back a json object.
contentClient.entries(query, function(err, entries){
if (err) throw err;
console.log(entries);
});
I've just been receiving the following error:
[TypeError: Converting circular structure to JSON]
The object is massive (over 3000 lines when I export it from the console to a document). So I can't post it here, but I am wondering if there is a way to find where the circular structure issue is within the object and how I remedy this?
I'm a developer at Contentful and I think I can help with the second part of your question.
As for the first part, greuze's answer is the ideal thing to do if you're in node land. An alternative (that can also be helpful in the browser) is using https://www.npmjs.com/package/safe-json-stringify
As for the second part, a thing the contentful.js library does is resolve links to other entries. The raw JSON contains just an object with metadata for links, but the linked entries come in an attached includes property. The library then goes and resolves those so you don't have to do it yourself.
However, we do allow you to create circular links when linking entries to each other (and you can even link an entry to itself!) and right now we haven't implemented a good way to detect and present those in the CMS (although that's a nice feature idea that I'll propose).
So once you do find that Circular reference, that should be your root issue.
In node 0.10 and 0.12, you can do:
var obj = {"child": {}};
obj.obj = obj;
util.inspect(obj, {depth: null})
and you will get something like:
'{ child: {}, obj: [Circular] }'
Depth indicates how many times to recurse while formatting the object (2 by default), null indicates indefinitely.
To find where are circular references, it is pretty easy to look for "[Circular]" in the resulting string.
I'm not sure if this is a performant solution, but it works. We're getting this issue only when we render things on the server side using Next.js.
We have an articles model that requires related articles which end up being linked and so this crops up quite often.
Here's what I did to solve the problem:
let article = await contentful.getArticle({
'fields.slug': query.slug,
include: 2,
})
const circularRef = _.get(article, 'items[0].fields.relatedArticle.fields.relatedArticle')
if (circularRef) {
delete article.items[0].fields.relatedArticle.fields.relatedArticle
}
Note that getArticle is a helper method I created and get is from the lodash library. Hope that helps.

Meteor Collections are not displaying

I'm following the book Getting Started With Meteor and I'm really not getting far because simple errors keep blocking me.
At this point in time I've started writing the initial app in the book in which we make a new global connection.
Lists = new Meteor.Collection("lists");
We then add some data to that collection.
lists.insert({Category:"DVDs", items: {Name:"Item Name",Owner:"me",LentTo:"Internet"}})
I can verify that the data is entered by checking in the console
lists.find({}).count(); //returns 2
lists.findOne({Category:"DVDs"}) //returns the DVD category
However when I try to display this content in the DOM nothing is displayed.
<div id="categories-container">
{{> categories}}
</div>
<template name="categories">
<div class="title"><h3>My Stuff</h3></div>
<div id="categories">
{{#each lists}}
<div class="category">
{{Category}}
</div>
{{/each}}
</div>
</template>
This displays only my Title. I get no errors in the browser console or the command line console. Not sure how to diagnose this.
I am pretty sure the reason is because you have
Lists = new Meteor.Collection("lists");
But then you do:
lists.insert({Category:"DVDs", items: {Name:"Item Name",Owner:"me",LentTo:"Internet"}})
lists.find({}).count(); //returns 2
lists.findOne({Category:"DVDs"}) //returns the DVD category
But you should do
Lists.insert({Category:"DVDs", items: {Name:"Item Name",Owner:"me",LentTo:"Internet"}})
Lists.find({}).count(); //returns 2
Lists.findOne({Category:"DVDs"}) //returns the DVD category
Because it is case sensitive. Then in your Template helper do a Lists.find({}) and you should be good to go.
Did you define a template helper to display your content?
You may need:
Template.categories.lists = function() {
return Lists.find({});
};
Check out the documentation for specifics:
http://docs.meteor.com/#templates
For faceting on categories, you'll probably want to set a reactive session value.
To make your time with the book Discover Meteor easier: If you have software that will compare two directories, get the book from git in a parallel directory to what you are typing in. Then, when you have problems, go to that directory in the terminal and git checkout the chapter. Now, compare the two folders and you'll see your spelling errors.
Learning quickly evolving stuff on the internet is a hard process. A lot of the tutorials you find only work for a period of time.
But the meteor book is different. They keep the code up. I personally typed along, and found my errors were solved on a better read (and often less thinking I knew what I was doing). I've talked two twenty-somethings through it, and they also consistently made new and creative punctuation or spelling choices for quite a while. There are also
meteor add xxx
meteor remove xxx
commands that are easy to miss.
But please trust that source (assuming you just got it, and aren't working from some old pdf) and doubt yourself, just for this tutorial. :)
By going off the comments here and by reading a bit more, the reasoning why was because there was nothing telling meteor that the template was defined.
This was solved by the following code:
Template.categories.lists = function (){
return Lists.find({}, {sort: {Category: 1}});
}
This tells it to find all of the records in the Lists collection and sort them by Category.

calling another list function in couchdb

Helo Folks,
I am working on a view in couchdb. And, in the 'extract' list function, I'm trying to filter out some information using that view (myView). From the client that connects to couchdb, I want to do 1 major thing - show the results from the 'extract' list function. But, there are multiple other things that I want to perform on the results returned from the 'extract' function. One simple operation out of all the other operations is 'sum'. But, there are many other features like calculating median/standard deviation etc on the results of the 'extract' list function.
{
"_id": "_design/myDesigndoc",
"lists": {
"extract": "function(head, req){ ...*extract some info the view*: **myView** ...}",
"sum" : "function(head,req) {...**sum up all the values returned from the 'extract' function above**...}"
},
"views": {
"myView" : { "map" : "..." },
}
}
So, I'm stuck at one point:-
As the whole design doc is a Json and the function bodies are javascript, is there a way to call the 'extract' list function in other list functions like 'sum', 'median', 'standard deviation' etc ?
Reason I want to do this:-
All the other list functions: 'sum', 'standard deviation' etc expect the return value of 'extract' function as input. So, just making redundant copies of the code of extract function in other list functions is the last thing I would want to do.
Is there an alternate way to solve this:-
Yes, there is a way. I had thought that I'll use another view functions than 'myView' for all these functionalities and write the same 'map' function as that in 'myView' but, all these views will have separate 'reduce' functions for calculating 'sum', 'standard dev' etc.
But, the calculation of those views caused a lot of resource usage because those many views were getting created each time.
Could you guys provide a better solution than this?
Thanks
My first thought was to implement the views again with reduce functions to do the calculations but you say this is too resource intensive.. I wonder how often are the views used and if there is a heap of changes between accesses?
If they are just used to produce some statistics for reports or something and are rarely accessed that when they do there is a heap of changes it needs to make to the view indexes maybe you could look at running a script that regularly retrieves the views so it keeps the views up to date so when they are accessed they still respond relatively quickly.
This is something we have done with our all of our views in our production environment and it works quite well, I guess it depends on your infrastructure and how much data you are pumping through.
Something else to consider I am not sure if there is any difference/benefit to doing so but maybe the built in reduce functions may offer better performance than your self created ones
http://wiki.apache.org/couchdb/Built-In_Reduce_Functions

Categories

Resources