Chapter 8. User Actions

Imagine 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 a 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 MongoDB shell, ensure that we’re referencing the notedly database (the name of the database in our .env file), and use MongoDB’s .remove() method. From your terminal, type the following:

$ mongo
$ use notedly ...

Get JavaScript Everywhere now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.