Since this is for a homework assignment I do not want to post my code because it essentially gives a solution. I can post some generic snippets. I am going to start off by saying I am new to javascript and Mongo, and basically learned them in a few hours last night.
Basically I have code that when I paste into the shell, it works perfectly, but when I save it into the database and try to execute it does not work. Here is a basic example.
db.system.js.save(
{
_id: "istrue",
value: function (x){
if(x == true)
print("true");
else
print("false");
}
})
So if I copy and paste this code and set var x = true or var x = false first then it works, but if I do this:
db.eval("istrue(true);");
Then it doesn't work.
Any ideas?
I can't find any documentation on this behavior, but a little testing reveals that your code is running just fine, but either stored functions can't use print or the output of print goes somewhere other than stdout. If you return strings instead of printing them, you'll see what you would expect.
I suspect the output is going to the MongoDB logs, but I'm not sure where my install put them.
Related
I'm a beginner in Node.js and also in programming. Perhaps I miss something very basic. I have two scripts which have to talk with each other in the command line. The first script gives numbers and the other makes some math and returns the answer. The first script was written by someone else and I don't know what is there. Te second script is mine. It reads the numbers in stdout and writes the answer in stdin. The first script "sees" the answer only after process.stdin.end(). But I have to read and write multiple times. What should I do?
My code looks like this:
var process = shell.exec('node first.js', {async:true});
process.stdout.on('data', function(data) {
process.stdin.write("" + evalStr(data) + "\n");
process.stdin.end();
});
Use process.stdin.write(data + "\n") instead of using evalStr().
I have a Jquery function in MVC View that check if at least one checkbox is clicked. Function is working properly if I use hardcoded string. But when I add
#Resources.myString into, it stops working, I can't figure out why
$('.form-horizontal').on('submit', function (e) {
if ($("input[type=checkbox]:checked").length === 0) {
e.preventDefault();
alert("This is working");
alert(#Resources.myString); //with this the function is not working anymore
return false;
}
});
I need to add the the string for multilingual purpose.
I tried diferent aproches
alert(#Resources.myString);
alert(#Html.Raw(Resources.myString))
var aaa = { #Html.Raw(Resources.myString)} //and calling the aaa
I think I am missing some basic knowlage of how this should work together
During page rendering, #Resources.myString will be injected as is in the code. For instance, if myString == "abc";, you'll end up with alert(abc); which is not what you want.
Just try to enclose your string in quotes:
alert("#Resources.myString");
As an aside, putting Razor code in Javascript logic is usually considered bad practice, as it prevents you from putting Javascript code in separate files (and therefore caching), and makes the code less readable.
Take a look as this question and the provided answer which gives a simple way to deal with that.
As ASP.NET dynamically generates HTML, CSS, JS code, the best way to find the error is to read the generated sources (Ctrl + U in most modern browsers).
You will see that your code
alert(#Resources.myString);
produces
alert(yourStringContent);
and should result in a console error yourStringContent is not defined.
You need to use quotes as you are working with a JavaScript string:
alert('#Resources.myString');
It will produce a correct JavaScript code like:
alert('yourStringContent');
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
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.