diff --git a/mongo.js b/mongo.js index e92eedb96bef32cb73d38b9d7f16727bd91062a6..6ffdfa6a957f58e3cf80071b79bd1d3ccaabd885 100644 --- a/mongo.js +++ b/mongo.js @@ -1,45 +1,39 @@ const mongoose = require('mongoose') // Replace with the URL of your own database. Do not store the password on GitHub! -const url = 'mongodb+srv://fullstack:xxx@clusterphonebook-duq8b.mongodb.net/fullstack-contacts' +const url = 'mongodb+srv://fullstack:XXX@clusterphonebook-duq8b.mongodb.net/fullstack-contacts' mongoose.connect(url, { useNewUrlParser: true }) +let mongo = mongoose.connection -process.argv.forEach((name, number) => { - var name = process.argv[2] - var number = process.argv[3] - console.log(`Adding person ${name} with the number ${number} to the directory`); -}); - - -const contactSchema = new mongoose.Schema({ +const Contact = mongoose.model('Contact',{ name: String, - number: String + number: String, + id: String }) -const Contact = mongoose.model('Contact', contactSchema); const contact = new Contact({ - name: 'Terho Tammi', - number: '666-555' -}) - - -contact - .save() - .then(response => { - console.log('contact saved!') - mongoose.connection.close() + name: process.argv[2], + number: process.argv[3] }) - -Contact - .find({}) - .then(result => { - result.forEach(contact => { - console.log(contact) +if (process.argv[2]){ + contact + .save() + .then(response => { + console.log(`Adding person ${process.argv[2]} with the number ${process.argv[3]} to the directory`) + mongo.close() + }) +} else { + Contact + .find({}) + .then(result => { + result.forEach(contact => { + console.log(contact) + }) + mongo.close() }) - mongoose.connection.close() - }) +}