Skip to content
Snippets Groups Projects
Commit 90dd89cb authored by Suvi Hyvönen's avatar Suvi Hyvönen
Browse files

Update mongo.js

parent 4be22b73
No related branches found
No related tags found
No related merge requests found
const mongoose = require('mongoose') const mongoose = require('mongoose')
// Replace with the URL of your own database. Do not store the password on GitHub! // 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 }) 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 Contact = mongoose.model('Contact',{
const contactSchema = new mongoose.Schema({
name: String, name: String,
number: String number: String,
id: String
}) })
const Contact = mongoose.model('Contact', contactSchema);
const contact = new Contact({ const contact = new Contact({
name: 'Terho Tammi', name: process.argv[2],
number: '666-555' number: process.argv[3]
}) })
if (process.argv[2]){
contact contact
.save() .save()
.then(response => { .then(response => {
console.log('contact saved!') console.log(`Adding person ${process.argv[2]} with the number ${process.argv[3]} to the directory`)
mongoose.connection.close() mongo.close()
}) })
} else {
Contact Contact
.find({}) .find({})
.then(result => { .then(result => {
result.forEach(contact => { result.forEach(contact => {
console.log(contact) console.log(contact)
}) })
mongoose.connection.close() mongo.close()
}) })
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment