pre-processing data into a hard-coded array - javascript

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

Related

cannot find a module from a different database in marklogic

OK i have a MarkLogic 9.0.2 database in which I have two applications deployed. Lets say A and B. Now I want to run some code inside B from A, we can do this with the xdmp.eval() but the code I want to run needs to find a module deployed in the modules database of B.
I cannot seem to get this working.
Code run inside database B works :
declareUpdate();
var prj = require('/root/lib/project-lib.xqy');
prj.createProject('giraffe', 'A project about giraffes');
finds and runs the createProject function in the module library...
Now from database A I try to run this by an xdmp.eval() like so:
declareUpdate();
var options = { "isolation" : "different-transaction",
"database" : xdmp.database("data-hub-FINAL"),
"modules" : xdmp.database("data-hub-MODULES")
}
xdmp.eval("declareUpdate();var prj = require('/root/lib/project-lib.xqy');prj.createProject('fromcluey giraffe', 'A project about giraffes from cluey');"
, options);
But gives me:
[javascript] XDMP-MODNOTFOUND: declareUpdate();var prj = require('/root/lib/project-lib.xqy');prj.createProject('fromcluey giraffe', 'A project about giraffes from cluey'); -- Module /root/lib/project-lib.xqy not found
Can someone tell me how I am supposed to find the project-lib.xqy module from inside A?
Document permissions was the first thing I was thinking of, which also applies to modules, schemas, triggers etc, not just to documents. Privileges second. Modules root can definitely be important as well.
However, there is a simple typo in the above xdmp.eval that is the biggest culprit here: the function takes 3 arguments, not 2. And options is the 3rd, not the second.
It should be: xdmp:eval("...", null, options).
HTH!

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.

'Async Juggling' - What is it really asking me to do?

I'm learning Node.js through the learnyounode project. I have completed the first few assignments and they all seemed reasonably straightforward.
Then, I got to the 'Async Juggling' one, and the assignment's description went completely over my head in terms of what I need to do.
The gist of it, is I need to write a Javascript that accepts 3 URLs as arguments, but that associates the correct response to the correct server. The assignment itself notes that you cannot naively assume that things will be properly associated with the correct URL.
The (incorrect) code I came up with proved that restriction true:
var http = require('http');
var bl = require('bl');
var httpCallback = function(response) {
var pipeHandler = function (err, data) {
if(err)
return console.error(err);
console.log(data.toString());
};
response.pipe(bl(pipeHandler));
};
var juggleAsyncConnections = function(connA, connB, connC) {
http.get(connA, httpCallback);
http.get(connB, httpCallback);
http.get(connC, httpCallback);
};
juggleAsyncConnections(process.argv[2], process.argv[3], process.argv[4]);
The problem, and thus my question, is, what is the correct way to handle asynchronous connection juggling, and what are the underlying concepts I need to understand to do it correctly?
Note: I've seen other questions, like "OMG why doesn't my solution work?" I'm not asking that, I deliberately set out to see the 'naive' solution fail for myself. I don't understand the underlying principles of why it doesn't work, or what principles actually do work. Additionally, I'm not asking for someone to 'solve the problem for me.' If the general algorithm can be explained, I can probably implement it on my own.
Counting callbacks is one of the fundamental ways of managing async in Node. [...]
That's an important piece.
You know how many inputs there are (3), and, because of that, you know how many outputs there should be. Keep a running tally as responses come back, then check if you received the full set before printing to the screen. You also want to keep the original order in mind (now if there were only a datatype that had numeric indexes... :grin:).
Good luck!

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