To store a contact into the json file from server.js file using a class in NodeJS












0















I have created three files -contactPresentation.js, contactService.js, contacts.json. I will run the contactService.js file. And it will navigate to contactPresentation.js file that will only choose an option to either add or display a file. If add(), then add the data into add of service file.And the data will be added to the contacts.json file . And if display(), then display the display() of service file. And the display will take the data from contacts.json file. However, i know my code is flawed. Please correct it . I am also getting an error -



        this.contacts.push(new ContactService(userName, contactNumber, emailId));
^

TypeError: Cannot read property 'push' of undefined
at ContactService.add (e:NodeJSProjectcontactManager2contactService.js:57:23)
at new ContactService (e:NodeJSProjectcontactManager2contactService.js:31:14)
at Object.<anonymous> (e:NodeJSProjectcontactManager2contactService.js:96:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)


contactService.js



const fs = require('fs');

var uuid = require('uuid');
const uuidv1 = require('uuid/v1');



console.log(uuid.v1());

eval(fs.readFileSync('contactPresentation.js').toString())

class ContactService {

constructor(userName, contactNumber, emailId) {


uuidv1();
this.id = uuid.v1();
this.firstName = userName;
this.contactNo = contactNumber;
this.email = emailId;
this.add(userName, contactNumber, emailId)
}

get UUID() {
return this.id;
}

get name() {
return this.firstName;
}

get contact() {
return this.contactNo;
}

get emailID() {
return this.email;
}
display() {

loadFromFile();
return this.contacts;
}

add(userName, contactNumber, emailId) {

this.contacts.push(new ContactService(userName, contactNumber, emailId));

saveToFile();

}

saveToFile() {

let data = ;

data.push(JSON.stringify(this.contacts, null, 2));

fs.writeFile('contacts.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});

console.log('This is after the write call');

}

loadFromFile() {

fs.readFile('contacts.json', (err, data) => {
if (err) throw err;
var contact = JSON.parse(data);
console.log("contacts", contact);
});

console.log('This is after the read call');
}

}

var test = new ContactService("abc",900, "abc@cateina.com");

test.display();
module.exports = ContactService


contactPresentation.js



contactService = require('./contactService.js')
var readlineSync = require('readline-sync');


var option = readlineSync.question(`What would you like to do, Add or Display Contact.
For Add , type 1, for display type 2 `);


class ContactPresentation {

constructor(option) {

abc = true;
createService = new ContactService();


while (abc) {
if (option == 1) {


var userName = readlineSync.question('May I have your name? ');

console.log('Hi ' + userName + '!');

var contactNumber = readlineSync.question('May I have your contact number');

var emailId = readlineSync.questionEMail();

createService.add(userName, contactNumber, emailId);

}
else if (option == 2) {
createService.display();
}

}
}
}

module.exports = ContactPresentation;


contacts.json



 









share|improve this question























  • eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

    – vibhor1997a
    Nov 24 '18 at 11:02











  • i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

    – user9630712
    Nov 24 '18 at 11:05













  • Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

    – vibhor1997a
    Nov 24 '18 at 11:06













  • yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

    – user9630712
    Nov 24 '18 at 11:08
















0















I have created three files -contactPresentation.js, contactService.js, contacts.json. I will run the contactService.js file. And it will navigate to contactPresentation.js file that will only choose an option to either add or display a file. If add(), then add the data into add of service file.And the data will be added to the contacts.json file . And if display(), then display the display() of service file. And the display will take the data from contacts.json file. However, i know my code is flawed. Please correct it . I am also getting an error -



        this.contacts.push(new ContactService(userName, contactNumber, emailId));
^

TypeError: Cannot read property 'push' of undefined
at ContactService.add (e:NodeJSProjectcontactManager2contactService.js:57:23)
at new ContactService (e:NodeJSProjectcontactManager2contactService.js:31:14)
at Object.<anonymous> (e:NodeJSProjectcontactManager2contactService.js:96:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)


contactService.js



const fs = require('fs');

var uuid = require('uuid');
const uuidv1 = require('uuid/v1');



console.log(uuid.v1());

eval(fs.readFileSync('contactPresentation.js').toString())

class ContactService {

constructor(userName, contactNumber, emailId) {


uuidv1();
this.id = uuid.v1();
this.firstName = userName;
this.contactNo = contactNumber;
this.email = emailId;
this.add(userName, contactNumber, emailId)
}

get UUID() {
return this.id;
}

get name() {
return this.firstName;
}

get contact() {
return this.contactNo;
}

get emailID() {
return this.email;
}
display() {

loadFromFile();
return this.contacts;
}

add(userName, contactNumber, emailId) {

this.contacts.push(new ContactService(userName, contactNumber, emailId));

saveToFile();

}

saveToFile() {

let data = ;

data.push(JSON.stringify(this.contacts, null, 2));

fs.writeFile('contacts.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});

console.log('This is after the write call');

}

loadFromFile() {

fs.readFile('contacts.json', (err, data) => {
if (err) throw err;
var contact = JSON.parse(data);
console.log("contacts", contact);
});

console.log('This is after the read call');
}

}

var test = new ContactService("abc",900, "abc@cateina.com");

test.display();
module.exports = ContactService


contactPresentation.js



contactService = require('./contactService.js')
var readlineSync = require('readline-sync');


var option = readlineSync.question(`What would you like to do, Add or Display Contact.
For Add , type 1, for display type 2 `);


class ContactPresentation {

constructor(option) {

abc = true;
createService = new ContactService();


while (abc) {
if (option == 1) {


var userName = readlineSync.question('May I have your name? ');

console.log('Hi ' + userName + '!');

var contactNumber = readlineSync.question('May I have your contact number');

var emailId = readlineSync.questionEMail();

createService.add(userName, contactNumber, emailId);

}
else if (option == 2) {
createService.display();
}

}
}
}

module.exports = ContactPresentation;


contacts.json



 









share|improve this question























  • eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

    – vibhor1997a
    Nov 24 '18 at 11:02











  • i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

    – user9630712
    Nov 24 '18 at 11:05













  • Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

    – vibhor1997a
    Nov 24 '18 at 11:06













  • yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

    – user9630712
    Nov 24 '18 at 11:08














0












0








0








I have created three files -contactPresentation.js, contactService.js, contacts.json. I will run the contactService.js file. And it will navigate to contactPresentation.js file that will only choose an option to either add or display a file. If add(), then add the data into add of service file.And the data will be added to the contacts.json file . And if display(), then display the display() of service file. And the display will take the data from contacts.json file. However, i know my code is flawed. Please correct it . I am also getting an error -



        this.contacts.push(new ContactService(userName, contactNumber, emailId));
^

TypeError: Cannot read property 'push' of undefined
at ContactService.add (e:NodeJSProjectcontactManager2contactService.js:57:23)
at new ContactService (e:NodeJSProjectcontactManager2contactService.js:31:14)
at Object.<anonymous> (e:NodeJSProjectcontactManager2contactService.js:96:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)


contactService.js



const fs = require('fs');

var uuid = require('uuid');
const uuidv1 = require('uuid/v1');



console.log(uuid.v1());

eval(fs.readFileSync('contactPresentation.js').toString())

class ContactService {

constructor(userName, contactNumber, emailId) {


uuidv1();
this.id = uuid.v1();
this.firstName = userName;
this.contactNo = contactNumber;
this.email = emailId;
this.add(userName, contactNumber, emailId)
}

get UUID() {
return this.id;
}

get name() {
return this.firstName;
}

get contact() {
return this.contactNo;
}

get emailID() {
return this.email;
}
display() {

loadFromFile();
return this.contacts;
}

add(userName, contactNumber, emailId) {

this.contacts.push(new ContactService(userName, contactNumber, emailId));

saveToFile();

}

saveToFile() {

let data = ;

data.push(JSON.stringify(this.contacts, null, 2));

fs.writeFile('contacts.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});

console.log('This is after the write call');

}

loadFromFile() {

fs.readFile('contacts.json', (err, data) => {
if (err) throw err;
var contact = JSON.parse(data);
console.log("contacts", contact);
});

console.log('This is after the read call');
}

}

var test = new ContactService("abc",900, "abc@cateina.com");

test.display();
module.exports = ContactService


contactPresentation.js



contactService = require('./contactService.js')
var readlineSync = require('readline-sync');


var option = readlineSync.question(`What would you like to do, Add or Display Contact.
For Add , type 1, for display type 2 `);


class ContactPresentation {

constructor(option) {

abc = true;
createService = new ContactService();


while (abc) {
if (option == 1) {


var userName = readlineSync.question('May I have your name? ');

console.log('Hi ' + userName + '!');

var contactNumber = readlineSync.question('May I have your contact number');

var emailId = readlineSync.questionEMail();

createService.add(userName, contactNumber, emailId);

}
else if (option == 2) {
createService.display();
}

}
}
}

module.exports = ContactPresentation;


contacts.json



 









share|improve this question














I have created three files -contactPresentation.js, contactService.js, contacts.json. I will run the contactService.js file. And it will navigate to contactPresentation.js file that will only choose an option to either add or display a file. If add(), then add the data into add of service file.And the data will be added to the contacts.json file . And if display(), then display the display() of service file. And the display will take the data from contacts.json file. However, i know my code is flawed. Please correct it . I am also getting an error -



        this.contacts.push(new ContactService(userName, contactNumber, emailId));
^

TypeError: Cannot read property 'push' of undefined
at ContactService.add (e:NodeJSProjectcontactManager2contactService.js:57:23)
at new ContactService (e:NodeJSProjectcontactManager2contactService.js:31:14)
at Object.<anonymous> (e:NodeJSProjectcontactManager2contactService.js:96:12)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:188:16)


contactService.js



const fs = require('fs');

var uuid = require('uuid');
const uuidv1 = require('uuid/v1');



console.log(uuid.v1());

eval(fs.readFileSync('contactPresentation.js').toString())

class ContactService {

constructor(userName, contactNumber, emailId) {


uuidv1();
this.id = uuid.v1();
this.firstName = userName;
this.contactNo = contactNumber;
this.email = emailId;
this.add(userName, contactNumber, emailId)
}

get UUID() {
return this.id;
}

get name() {
return this.firstName;
}

get contact() {
return this.contactNo;
}

get emailID() {
return this.email;
}
display() {

loadFromFile();
return this.contacts;
}

add(userName, contactNumber, emailId) {

this.contacts.push(new ContactService(userName, contactNumber, emailId));

saveToFile();

}

saveToFile() {

let data = ;

data.push(JSON.stringify(this.contacts, null, 2));

fs.writeFile('contacts.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});

console.log('This is after the write call');

}

loadFromFile() {

fs.readFile('contacts.json', (err, data) => {
if (err) throw err;
var contact = JSON.parse(data);
console.log("contacts", contact);
});

console.log('This is after the read call');
}

}

var test = new ContactService("abc",900, "abc@cateina.com");

test.display();
module.exports = ContactService


contactPresentation.js



contactService = require('./contactService.js')
var readlineSync = require('readline-sync');


var option = readlineSync.question(`What would you like to do, Add or Display Contact.
For Add , type 1, for display type 2 `);


class ContactPresentation {

constructor(option) {

abc = true;
createService = new ContactService();


while (abc) {
if (option == 1) {


var userName = readlineSync.question('May I have your name? ');

console.log('Hi ' + userName + '!');

var contactNumber = readlineSync.question('May I have your contact number');

var emailId = readlineSync.questionEMail();

createService.add(userName, contactNumber, emailId);

}
else if (option == 2) {
createService.display();
}

}
}
}

module.exports = ContactPresentation;


contacts.json



 






javascript node.js json






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 10:53







user9630712




















  • eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

    – vibhor1997a
    Nov 24 '18 at 11:02











  • i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

    – user9630712
    Nov 24 '18 at 11:05













  • Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

    – vibhor1997a
    Nov 24 '18 at 11:06













  • yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

    – user9630712
    Nov 24 '18 at 11:08



















  • eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

    – vibhor1997a
    Nov 24 '18 at 11:02











  • i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

    – user9630712
    Nov 24 '18 at 11:05













  • Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

    – vibhor1997a
    Nov 24 '18 at 11:06













  • yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

    – user9630712
    Nov 24 '18 at 11:08

















eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

– vibhor1997a
Nov 24 '18 at 11:02





eval(fs.readFileSync('contactPresentation.js').toString()) why are you evaling instead of requiring, importing,??

– vibhor1997a
Nov 24 '18 at 11:02













i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

– user9630712
Nov 24 '18 at 11:05







i just put that piece of code to navigate to contactPresentation.js file so that contactService.js file starts its wrking from Presentation file

– user9630712
Nov 24 '18 at 11:05















Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

– vibhor1997a
Nov 24 '18 at 11:06







Your code have circular dependency contact service depends on contact person and vice versa and evaling isn't a good idea.

– vibhor1997a
Nov 24 '18 at 11:06















yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

– user9630712
Nov 24 '18 at 11:08





yeah. So how do i start from service.js file and end up displaying the content of Presentation file first

– user9630712
Nov 24 '18 at 11:08












1 Answer
1






active

oldest

votes


















1














You have To déclare



this.contacts = ;


In your contactService constructor.



And also, to start do what you asked in comment, your index.js would look like :



const ContactPresentation = require('contactPresentation.js');
const ContactService = require('contactService.js');

// to run contactPresentation
const cp = new ContactPresentation();

// to run display the content of Presentation file
var test = new ContactService("abc",900, "abc@cateina.com");
test.display();


Of course, you need to remove the 'eval' line, var test line and test.display line from contactPresentation.js



That's as simple as that.






share|improve this answer


























  • after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

    – user9630712
    Nov 24 '18 at 11:03













  • I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

    – dun32
    Nov 24 '18 at 11:21













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457394%2fto-store-a-contact-into-the-json-file-from-server-js-file-using-a-class-in-nodej%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown
























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














You have To déclare



this.contacts = ;


In your contactService constructor.



And also, to start do what you asked in comment, your index.js would look like :



const ContactPresentation = require('contactPresentation.js');
const ContactService = require('contactService.js');

// to run contactPresentation
const cp = new ContactPresentation();

// to run display the content of Presentation file
var test = new ContactService("abc",900, "abc@cateina.com");
test.display();


Of course, you need to remove the 'eval' line, var test line and test.display line from contactPresentation.js



That's as simple as that.






share|improve this answer


























  • after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

    – user9630712
    Nov 24 '18 at 11:03













  • I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

    – dun32
    Nov 24 '18 at 11:21


















1














You have To déclare



this.contacts = ;


In your contactService constructor.



And also, to start do what you asked in comment, your index.js would look like :



const ContactPresentation = require('contactPresentation.js');
const ContactService = require('contactService.js');

// to run contactPresentation
const cp = new ContactPresentation();

// to run display the content of Presentation file
var test = new ContactService("abc",900, "abc@cateina.com");
test.display();


Of course, you need to remove the 'eval' line, var test line and test.display line from contactPresentation.js



That's as simple as that.






share|improve this answer


























  • after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

    – user9630712
    Nov 24 '18 at 11:03













  • I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

    – dun32
    Nov 24 '18 at 11:21
















1












1








1







You have To déclare



this.contacts = ;


In your contactService constructor.



And also, to start do what you asked in comment, your index.js would look like :



const ContactPresentation = require('contactPresentation.js');
const ContactService = require('contactService.js');

// to run contactPresentation
const cp = new ContactPresentation();

// to run display the content of Presentation file
var test = new ContactService("abc",900, "abc@cateina.com");
test.display();


Of course, you need to remove the 'eval' line, var test line and test.display line from contactPresentation.js



That's as simple as that.






share|improve this answer















You have To déclare



this.contacts = ;


In your contactService constructor.



And also, to start do what you asked in comment, your index.js would look like :



const ContactPresentation = require('contactPresentation.js');
const ContactService = require('contactService.js');

// to run contactPresentation
const cp = new ContactPresentation();

// to run display the content of Presentation file
var test = new ContactService("abc",900, "abc@cateina.com");
test.display();


Of course, you need to remove the 'eval' line, var test line and test.display line from contactPresentation.js



That's as simple as that.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 '18 at 13:01

























answered Nov 24 '18 at 10:56









dun32dun32

44035




44035













  • after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

    – user9630712
    Nov 24 '18 at 11:03













  • I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

    – dun32
    Nov 24 '18 at 11:21





















  • after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

    – user9630712
    Nov 24 '18 at 11:03













  • I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

    – dun32
    Nov 24 '18 at 11:21



















after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

– user9630712
Nov 24 '18 at 11:03







after adding it, i get error - bth[buf[i++]], bth[buf[i++]]]).join(''); RangeError: Maximum call stack size exceeded

– user9630712
Nov 24 '18 at 11:03















I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

– dun32
Nov 24 '18 at 11:21







I don't understand why you use an eval instead of a require. And also, contactService.js don't need contactPresentation reference. I'd create an index.js including all the needed references, I won't run contactPresentation from contactService.js

– dun32
Nov 24 '18 at 11:21






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53457394%2fto-store-a-contact-into-the-json-file-from-server-js-file-using-a-class-in-nodej%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Redirect URL with Chrome Remote Debugging Android Devices

Dieringhausen