I am no JavaScript expert, but after a long time of reading, experimenting and the power of Google and especially the results of stackoverflow I managed to work with the data of a REST API.
I made everything with CodePen, because it's nice to understand what I was doing.
Now I came to a problem that I can't solve myself. I tried to google it, but I think I just don't know the right keywords.
My problem is a SyntaxError which only occurs in debug mode. But I think my main problem is, that I understand how to get all values and work with all Object.keys and Object.values but not the correct way to get 1 specific value.
Uncaught SyntaxError: Unexpected token '['
The JSON file looks like this:
[{"label":"Value","next":Value, ...},{"label":"Value","next":Value, ...}, ...]
My JavaScript Code looks like this:
const api_url = 'external json file';
async function getBrowsersData() {
const response_api_url = await fetch(api_url_api_url);
const data_api_url = await response_api_url.json();
const data = data_api_url.[0].label;
The SyntaxError comes from the [0], what is the explained way here, if I understand it right.
If I use ["0"] as described here, I get a SyntaxError in CodePen without Debug Mode.
Can someone please explain how I select the first label and, more importantly, why this works in CodePen, but not in its debug mode?
A link with a detailed description would help me too. I know it's probably a very simple question, but I am stuck and out of keywords I could search.
I have read this excellent and very detailed answer several times, but I do not understand my mistake.
Can you provide the codepen? It's hard to see exactly what you're doing wrong without full sample data
A quick issue I see is this though:
const data = data_browsers_30.[0].label;
should probably be:
const data = data_browsers_30[0].label;
That's the reason you get the SyntaxError; you don't need the . before [0].
if you think the label key is the issue this syntax is generally also valid:
const data = data_browsers_30[0]["label"]
Related
I have been working on my project where I had to do some updates on my data records. After I finished my update I got an error: SyntaxError: identifier starts immediately after numeric literal and this line of code was below that error in firebug : maxScores.ew-19a = ''
I looked up in my code and I found where is this output coming from, here is the code:
var maxScores = new Object;
<cfoutput query="getRec">maxScores.#LCase(tCode)# = '#maxScore#';</cfoutput>
In my update I had to put - symbol between letter and number, in old data I did not have that so I think that causing the problem here. I was wondering how I can prevent this or if there is any method that I have to put around my output to prevent this? If you know how this can be fixed pleas let me know. Thank you.
ColdFusion will attempt to subtract ew from 19a when you just dump it between to pound signs like that. You will need to use bracket/object notation here. Try this:
<cfoutput query="getRec">
maxScores.#LCase(getRec["tCode"][currentrow])# = '#getRec["maxScore"][currentrow]#';
</cfoutput>
If you want to Lower case something do it in the query. Outputing JS using CF is useful and solves many problems, but you want to keep it as clean as possible to keep your brain from fogging over. :)
Mongodb is cool enough to create the database/collection on the fly, if we run a code similar to
db.store.save({a: 789});
It automatically creates store collection and add a document to it.
My javascript understanding says it is not possible to call a method on an undefined property of db object. It should have resulted in some kind of error/exception.
I am curious to understand the happenings behind the scene and if there is any helpful link please point me to those. Googling did not help me much.
In JavaScript there is a way to define a function that will be executed when an undefined method is called.
Example:
var o = {
__noSuchMethod__: function(id, args) { console.log(id, '(' + args.join(', ') + ')'); }
};
o.foo(1, 2, 3);
o.bar(4, 5);
o.baz();
// Output
// foo (1, 2, 3)
// bar (4, 5)
// baz ()
Note this is a non-standard feature and today only works in Firefox.
I do not know how MongoDB implemented this feature, but I'm just responding in order to report that can be done this way.
Fot more details see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/noSuchMethod
As I recall in a NodeJS environment you must do something like this to actually create a record: db.get('collectionName').insert({..something...}); or db.get('collectionName').save({...something...}); but you don't get to use the collection name as a property of db.
The line you're mentioning is only used in MongoDB shell, which is not Javascript.
I guess you're misunderstanding what's MongoDB shell and what's a MongoDB driver.
So long story short MongoDB (driver) is not able to access an undefined property.
EDIT
In response to your comment..
MongoDB JS driver's GitHub page pretty much points out how to insert a field and always uses the syntax I mentioned: https://github.com/mongodb/node-mongodb-native
As for what you're using in the shell it's pretty clear that you can't just use Javascript in a command shell. So I guess I'll point you to a place in which you can see in what language was MongoDB developed: http://www.mongodb.org/ pretty much the first line says it's written in C++.
Hope this helps clarify your question
I am trying to retrieve random 200 objects from the array returned to me by query.find() method. First i tried to implement all random number generation and all . And just now i got introduced to underscore.js method "_.sample" .
But something is going wrong. I dont have much knowledge of underscore.js. So if someone could help that will be great.
When i try to sun _.sample method it give me the error :
TypeError: Object function (e){if(e instanceof T)return e;if(!(this instanceof T))return new T(e);this._wrapped=e} has no method 'sample'
Someone please explain what exactly this error is. I tried searching but didn't get explanatory content. Thank you in advance.
Here's the code :
var queryPhrases = new Parse.Query("Phrases");
queryPhrases.select("phraseId");
queryPhrases.find().then(function(phrases){
var arrayOfUnused = _.sample(phrases,request.params.count);
user.add("usedPhrases",arrayOfUnused);
user.save();
response.success(arrayOfUnused) ;
});
Parse Cloud Code includes an outdated version of Underscore, but frustratingly, I can't find anything stating which version. While Underscore no longer ships with the Parse Javascript SDK as of v1.6.0 (late 2015), previously it had only used UnderscoreJS v1.4.4 (early 2013), so I'd expect Cloud Code to be using something from around then.
Adding the latest Underscore source to your Cloud Code is always an option, and then require it like any other of your own files.
Alternatively, I used the following to show a list of functions available in the included Underscore Cloud Code module.
var _ = require('underscore');
var availableFunctions = _.functions(_);
console.log('Available Underscore Functions: ' + JSON.stringify(availableFunctions));
Thanks a lot guys for your responses. I couldn't find the reason or the solution to run _.sample in my code. So i implemented it the other way. Here's what i did.
var arrayOfUnused = _.first(_.shuffle(phrases),request.params.count);
This works. :-)
Hei
I am going through the JavaScript tutorial on Codeacademy and I'm stuck on Introduction to Objects II lesson 2/30. The code that I have entered seems fine to me and the code prints the necessary line hello to the console.
But I get an error "Oops, try again. It looks like 'Hello!' wasn't logged to the console. Make sure that you properly defined the method and that you didn't change any of the provided code."
I cant seem to find anything wrong with this code that I have entered
function Person(job, married) {
this.job = job;
this.married = married;
// add a "speak" method to Person!
this.speak = function() {
console.log("Hello");
};
}
var user = new Person("Codecademy Student",false);
user.speak();
The problem is in your posted image, see the last line of the code editor:
user.speak();z //<-- z is not something what you have defined.
I went through several courses on Codeacademy. Codeacademy often has broken lessons, and if it's working on jsFiddle, it's likely two things.
1: Spelling and punctuation. Codeacademy is very specific with strings. One wrong letter, or one wrongly punctuated letter will show it as a fail.
2: Error. If this is the case, the codeacademy community usually has work arounds. If not, you can skip this particular lesson, and keep on going with the course. The 100% complete is more symbolic than anything else. As long as you're learning the concepts, it's find to skip whatever you have to.
Also, codeacademy has an excellent community that will give more specific advice tailored to the course. Here's the relevant forum for that course.
http://www.codecademy.com/forums/objects-ii/0
following code works properly
draw([['Rice',20,28,38],['Paddy',31,38,55],]);
but when i try using external variable like
var val1=20;
var val2=30;
var val3=40;
draw([['Rice',val1,val2,val3],['Paddy',31,38,55],]);
It wont work.
Just showing that your example code works fine using the Firebug console. Can you post more of your code? Your stripped-down example is probably missing something else that's causing a problem.
What is your draw() function doing? Could something in that function be breaking?
EDIT: Another problem could be the trailing comma after your second array. That will throw an error in Internet Explorer.
alert([['Rice',val1,val2,val3],['Paddy',31,38,55],]);
should be:
alert([['Rice',val1,val2,val3],['Paddy',31,38,55]]);
That may solve your issue (though you also have that in your 'working' example, but I thought it worth mentioning).
Your code snippets are not equivalent -- the second one has different values (['Rice',20,30,40] vs ['Rice',20,28,38]). Other than that, they are equivalent and should have the same effects.