Skip to content

Instantly share code, notes, and snippets.

@nolanlawson
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nolanlawson/0f1c815cb5fe74cff5fc to your computer and use it in GitHub Desktop.
Save nolanlawson/0f1c815cb5fe74cff5fc to your computer and use it in GitHub Desktop.
Incrementing Charlie's age, with an error
<html>
<body>
<pre id="display"></pre>
<script src="//cdn.jsdelivr.net/pouchdb/3.1/pouchdb.min.js"></script>
<script src="index.js"></script>
</body>
</html>
// Destroy the database before doing anything, because I want
// you to see the same thing if you reload.
// Ignore the man behind the curtain!
new PouchDB('sample').destroy().then(function () {
return new PouchDB('sample');
}).then(function (db) {
//
// IMPORTANT CODE STARTS HERE
//
db.put({_id: 'charlie', age: 21}).then(function () {
return db.get('charlie');
}).then(function (charlie) {
document.getElementById('display').innerHTML = 'Charlie is how old? ' + JSON.stringify(charlie);
// increment Charlie's age
charlie.age++;
return db.put(charlie);
}).then(function () {
return db.get('charlie');
}).then(function (charlie) {
document.getElementById('display').innerHTML += '\nCharlie is how old? ' + JSON.stringify(charlie);
// increment Charlie's age again
charlie.age++;
return db.put(charlie);
}).then(function () {
return db.get('carlie'); // <-- WHOOPS, THIS IS A TYPO!
}).then(function (charlie) {
document.getElementById('display').innerHTML += '\nCharlie is how old? ' + JSON.stringify(charlie);
}).catch(function (err) {
console.log(err);
document.getElementById('display').innerHTML += '\nWHOOPS WE GOT AN ERROR: ' + err;
});
//
// IMPORTANT CODE ENDS HERE
//
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment