Skip to content

Commit 4509211

Browse files
committedJan 26, 2016
Changed for Part 2
1 parent a9e5ac4 commit 4509211

File tree

3 files changed

+18
-10
lines changed

3 files changed

+18
-10
lines changed
 

‎public/main.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* globals fetch */
2-
32
var update = document.getElementById('update')
43
var del = document.getElementById('delete')
4+
55
update.addEventListener('click', function () {
66
fetch('quotes', {
77
method: 'put',
@@ -10,8 +10,12 @@ update.addEventListener('click', function () {
1010
'name': 'Darth Vader',
1111
'quote': 'I find your lack of faith disturbing.'
1212
})
13-
}).then(function (response) {
14-
window.location.reload()
13+
})
14+
.then(response => {
15+
if (response.ok) return response.json()
16+
})
17+
.then(data => {
18+
console.log(data)
1519
})
1620
})
1721

‎server.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const express = require('express')
22
const app = express()
33
const bodyParser = require('body-parser')
4-
54
const MongoClient = require('mongodb').MongoClient
5+
66
var db
77

88
MongoClient.connect('mongodb://zellwk:zellwk@ds047955.mongolab.com:47955/star-wars-quotes', (err, database) => {
@@ -16,7 +16,7 @@ MongoClient.connect('mongodb://zellwk:zellwk@ds047955.mongolab.com:47955/star-wa
1616
app.set('view engine', 'ejs')
1717
app.use(bodyParser.urlencoded({extended: true}))
1818
app.use(bodyParser.json())
19-
app.use(express.static(__dirname + '/public'))
19+
app.use(express.static('public'))
2020

2121
app.get('/', (req, res) => {
2222
db.collection('quotes').find().toArray((err, result) => {
@@ -35,14 +35,17 @@ app.post('/quotes', (req, res) => {
3535

3636
app.put('/quotes', (req, res) => {
3737
db.collection('quotes')
38-
.findOneAndUpdate({}, {
38+
.findOneAndUpdate({name: 'Yoda'}, {
3939
$set: {
4040
name: req.body.name,
4141
quote: req.body.quote
4242
}
43+
}, {
44+
sort: {_id: -1},
45+
upsert: true
4346
}, (err, result) => {
44-
if (err) return res.send(500, err)
45-
res.send(result.value)
47+
if (err) return res.send(err)
48+
res.send(result)
4649
})
4750
})
4851

‎views/index.ejs

+3-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
</form>
2727

2828
<div>
29-
<h2>Change first quote</h2>
30-
<button id="update"> Change quote </button>
29+
<h2>Replace last quote written by Master Yoda with a quote written by Darth Vadar</h2>
30+
<button id="update"> Darth Vadar invades! </button>
3131
</div>
32+
3233
<div>
3334
<h2>Delete Darth Vadar's first quote</h2>
3435
<button id="delete"> Delete first Darth Vadar quote </button>

0 commit comments

Comments
 (0)
Please sign in to comment.