O'Reilly logo

JavaScript Everywhere by Adam D. Scott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 8. User Actions

Imagine if you had just joined a club (remember the “Secret Club for Super Cool People?”), but when you showed up for the first time there was nothing to do. The club was a big, empty room with people wandering in and out with no way of interacting with the club or one another. I’m a bit of an introvert, so this doesn’t sound that bad, but I wouldn’t be willing to pay a membership fee for it.

Right now our API is essentially this big, useless club. We have a way to create data and a way for users to sign in, but nothing allows a user to own that data. In this chapter, we’ll be addressing this by adding user interactions. We’ll write the code that will enable a user to own the notes they create, limit who can delete or modify a note, and enable users to “favorite” a note that they like. Additionally, we’ll enable API users to make nested queries, allowing our UIs to write simple queries that relate users to notes.

Before We Get Started

In this chapter, we’ll be making some pretty significant changes to our notes files. Since we have a small amount of data in our database, you may find it easier to remove the existing notes from your local database. This isn’t necessary, but can reduce confusion as you work through this chapter.

To do this, we’ll go into the mongo shell, ensure that we’re referencing the notedly database, and use MongoDB’s .remove() method. From your terminal, type the following:

$ mongo
$ use notedly
$ db.notes.remove({})

Attach a User ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required