Firstly, I am a noob to node.js and JavaScript.
I am trying to write a program that stores the notes of a user in the local system using yargs.
My train of through in the handler is as follows-
First create the an object that contains the note's title and its contents.
I assume that file is always created in the user's local system and is either going to be empty or have some notes.
Get the contents of the file and if it is empty, create an object that has the array of all notes and push the note to it. I then write that to file after doing the necessary JSON manipulations.
If an object is already present in the file, I just push the new note to it.
Here is my code
const yargs = require("yargs")
const fs = require("fs")
yargs.command({
command: "add",
describe: "Add a note",
builder: {
title: {
describe: "Title of the note to add",
demandOption: true,
type: "string"
},
note: {
describe: "Contents of the note to add",
demandOption: true,
type: "string"
}
},
handler: function() {
let fullNote = {
title: yargs.argv.title,
note: yargs.argv.note
}
let fileContents = JSON.parse(fs.readFileSync("notes.json").toString())
if(fileContents === undefined || fileContents === null){
let newArrayJSON = {
notes: []
}
newArrayJSON.notes[0] = JSON.stringify(fullNote)
fs.writeFileSync("notes.json", newArrayJSON)
} else {
fileContents.notes.push(JSON.stringify(fullNote))
fs.writeFileSync("notes.json", newArrayJSON)
}
}
})
yargs.parse()
And this is the error message I get
PS D:\Documents\Projects\Node\Node-NotesApp> node app.js add --title="To Buy" --note="Eggs"
D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1242
else throw err
^
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at Object.handler (D:\Documents\Projects\Node\Node-NotesApp\app.js:34:33)
at Object.runCommand (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\lib\command.js:240:40)
at Object.parseArgs [as _parseArgs] (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:1154:41)
at Object.parse (D:\Documents\Projects\Node\Node-NotesApp\node_modules\yargs\yargs.js:599:25)
at Object.<anonymous> (D:\Documents\Projects\Node\Node-NotesApp\app.js:54:7)
at Module._compile (internal/modules/cjs/loader.js:1200:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1220:10)
at Module.load (internal/modules/cjs/loader.js:1049:32)
at Function.Module._load (internal/modules/cjs/loader.js:937:14)
I have tried so many hings but cannot get rid of that error.
Could someone help me. Any other helpful information/resources to help me better understand this concept would be greatly appreciated.
Any explanations/resources on working with JSON in node (like JSON.stringify, JSON.parse ) would be be very helpful.
Thanks in advance
I solved it by populating the file with an empty array.
The error pops up when the file is empty as #GuyIncognito pointed out.
Thanks everyone for the help
I am trying to understand the value of this at different points in a script. Questions similar to mine have been answered in this forum but those answers are considerably above my current learning level.
In my code experiments, I am using console.logs to return the this value. The value returned is always as expected, but the format of the returned value is inconsistent, which leads me to wonder why.
This code returns the expected Window object for the first 3 log commands to be executed but returns only the object literal for the 4th command, executed from the object's method.
var myName = {
name: "James",
sayName: function() {
console.log(this, 4);
console.log(this.name)
}
}
console.log(this, 1);
function myFunction() {
console.log(this, 2);
function nestFunction() {
console.log(this, 3);
myName.sayName();
}
nestFunction();
}
myFunction();
I have 3 questions: Why doesn't console.log return the name of the object? Is there a way to make it do so? Is there a simple way to do that other than console.log? Any help would be appreciated.
Ok I was going through your code to see what you specifically mean
here is the short explanation as to why THIS is different in some of the places
This keyword refers to the object it belongs to. Generally you used it to refer to the global window object .That's what is reflecting in your console log 1,2,3 .
Calling this in static javaScript object will return the javaScript object ,not the window object that is what is reflecting in the console.log(this,4).
So it gives you a way to call elements inside a static object .
Another way to understand this keyword is to look at constructors .The best example of the keyword
this
is inside a constructor function
var myObj = function(){
function myObj(ref)
{
this.name = "";
this.Item = "";
this.ref = ref;
this.speak();
}
myObj.prototype.speak =function()
{
this.name = 'James';
this.item = 'cheese';
console.log(this.ref)
//and the constuctor object
console.log(this)
}
return myObj;
}();
var e = new myObj('a Refrence string');
This should give you a basic understanding of how this works
here is more info to get you started Wschools.com
I’ll start with the code:
var s = ["hi"];
console.log(s);
s[0] = "bye";
console.log(s);
Simple, right? In response to this, the Firefox console says:
[ "hi" ]
[ "bye" ]
Wonderful, but Chrome’s JavaScript console (7.0.517.41 beta) says:
[ "bye" ]
[ "bye" ]
Have I done something wrong, or is Chrome’s JavaScript console being exceptionally lazy about evaluating my array?
Thanks for the comment, tec. I was able to find an existing unconfirmed Webkit bug that explains this issue: https://bugs.webkit.org/show_bug.cgi?id=35801 (EDIT: now fixed!)
There appears to be some debate regarding just how much of a bug it is and whether it's fixable. It does seem like bad behavior to me. It was especially troubling to me because, in Chrome at least, it occurs when the code resides in scripts that are executed immediately (before the page is loaded), even when the console is open, whenever the page is refreshed. Calling console.log when the console is not yet active only results in a reference to the object being queued, not the output the console will contain. Therefore, the array (or any object), will not be evaluated until the console is ready. It really is a case of lazy evaluation.
However, there is a simple way to avoid this in your code:
var s = ["hi"];
console.log(s.toString());
s[0] = "bye";
console.log(s.toString());
By calling toString, you create a representation in memory that will not be altered by following statements, which the console will read when it is ready. The console output is slightly different from passing the object directly, but it seems acceptable:
hi
bye
From Eric's explanation, it is due to console.log() being queued up, and it prints a later value of the array (or object).
There can be 5 solutions:
1. arr.toString() // not well for [1,[2,3]] as it shows 1,2,3
2. arr.join() // same as above
3. arr.slice(0) // a new array is created, but if arr is [1, 2, arr2, 3]
// and arr2 changes, then later value might be shown
4. arr.concat() // a new array is created, but same issue as slice(0)
5. JSON.stringify(arr) // works well as it takes a snapshot of the whole array
// or object, and the format shows the exact structure
You can clone an array with Array#slice:
console.log(s); // ["bye"], i.e. incorrect
console.log(s.slice()); // ["hi"], i.e. correct
A function that you can use instead of console.log that doesn't have this problem is as follows:
console.logShallowCopy = function () {
function slicedIfArray(arg) {
return Array.isArray(arg) ? arg.slice() : arg;
}
var argsSnapshot = Array.prototype.map.call(arguments, slicedIfArray);
return console.log.apply(console, argsSnapshot);
};
For the case of objects, unfortunately, the best method appears to be to debug first with a non-WebKit browser, or to write a complicated function to clone. If you are only working with simple objects, where order of keys doesn't matter and there are no functions, you could always do:
console.logSanitizedCopy = function () {
var args = Array.prototype.slice.call(arguments);
var sanitizedArgs = JSON.parse(JSON.stringify(args));
return console.log.apply(console, sanitizedArgs);
};
All of these methods are obviously very slow, so even more so than with normal console.logs, you have to strip them off after you're done debugging.
This has been patched in Webkit, however when using the React framework this happens for me in some circumstances, if you have such problems just use as others suggest:
console.log(JSON.stringify(the_array));
Looks like Chrome is replacing in its "pre compile" phase any instance of "s" with pointer to the actual array.
One way around is by cloning the array, logging fresh copy instead:
var s = ["hi"];
console.log(CloneArray(s));
s[0] = "bye";
console.log(CloneArray(s));
function CloneArray(array)
{
var clone = new Array();
for (var i = 0; i < array.length; i++)
clone[clone.length] = array[i];
return clone;
}
the shortest solution so far is to use array or object spread syntax to get a clone of values to be preserved as in time of logging, ie:
console.log({...myObject});
console.log([...myArray]);
however be warned as it does a shallow copy, so any deep nested non-primitive values will not be cloned and thus shown in their modified state in the console
This is already answered, but I'll drop my answer anyway. I implemented a simple console wrapper which doesn't suffer from this issue. Requires jQuery.
It implements only log, warn and error methods, you will have to add some more in order for it to be interchangeable with a regular console.
var fixedConsole;
(function($) {
var _freezeOne = function(arg) {
if (typeof arg === 'object') {
return $.extend(true, {}, arg);
} else {
return arg;
}
};
var _freezeAll = function(args) {
var frozen = [];
for (var i=0; i<args.length; i++) {
frozen.push(_freezeOne(args[i]));
}
return frozen;
};
fixedConsole = {
log: function() { console.log.apply(console, _freezeAll(arguments)); },
warn: function() { console.warn.apply(console, _freezeAll(arguments)); },
error: function() { console.error.apply(console, _freezeAll(arguments)); }
};
})(jQuery);
I'm using Breeze and Angular on client and NHibernate on server.For business logic reasons I need to know if some object has changes after it has been saved.For that reasons I'm calling
manager.hasChanges(['ArrayOfTypes']);
In case when object is loaded from db it works as expected(returns true/false),
but after object is saved through
manager.saveChanges(['ArrayOfTypes']);
I get this error:
TypeError: Cannot read property 'hasChanges' of undefined
The error gets thrown here in breeze.js file:
proto._hasChangesCore = function(entityTypes) {
entityTypes = checkEntityTypes(this, entityTypes);
var entityGroups = getEntityGroups(this, entityTypes);
return entityGroups.some(function (eg) {
return eg.hasChanges();
});
};
I'm expecting 32 entityGroups to return and there are indeed 32 elements in array, but 25 of them are undefined.For now I've made a temp fix which does not feel right at all:
proto._hasChangesCore = function(entityTypes) {
entityTypes = checkEntityTypes(this, entityTypes);
var entityGroups = getEntityGroups(this, entityTypes);
return entityGroups.some(function (eg) {
if (eg !== undefined) {
return eg.hasChanges();
}
else {
return false;
}
});
};
Is there a way to deal with problem in other way?
Thanks
This happens when the one or more of the entity types is not represented by an EntityGroup in the entity manager. But you should be able to pass in any type name and not have it blow up.
This is a bug in Breeze, which is finally fixed. The fix will be in the next release.
I have two files in Node.js where one requires the other one.
variable_test.js:
TEST = require('./variable_test_external.js');
TEST.get(function(myVariable) {
var changeMeVariable;
console.log(myVariable);
changeMeVariable = myVariable.epicVariable;
changeMeVariable.evenEpicerVariable = "test3";
TEST.get(function(myVariable2) {
console.log(myVariable2);
});
});
variable_test_external.js:
var testVariable = new Array({epicVariable: {evenEpicerVariable: "test1"}}, {epicVariable: {evenEpicerVariable: "test2"}});
exports.get = function(callback) {
callback(testVariable[1]); // I know that the return is unnecessary in this example but in my real application I have return there for compactness.
}
This is the output when run in Node.js with node variable_test.js:
{ epicVariable: { evenEpicerVariable: 'test2' } }
{ epicVariable: { evenEpicerVariable: 'test3' } }
The console.log(myVariable) changes in the two TEST.get's. Why does this happen?
This is a reference copy, not a value copy. You got the object from the array, NOT a copy of them.
changeMeVariable = myVariable.epicVariable;
This would have to fix yout problem
// PSEUDO CODE, i don't know the correct syntax
changeMeVariable = {
epicVariable = myVariable.epicVariable
};
The answer in my case is the following based on the links at the bottom:
changeMeVariable = JSON.parse(JSON.stringify(myVariable.epicVariable));
But, it's much better to manually copy it like the bottom most link like this:
changeMeVariable = {
evenEpicerVariable: myVariable.epicVariable.evenEpicerVariable
}
n0m's answer is similar but if the epicVariable.evenEpicerVariable contained an object that object's reference would still be linked! (I tested it)
References:
What is the most efficient way to deep clone an object in JavaScript?
http://jsperf.com/cloning-an-object/3