Author: Robbert Kauffman
Modern web applications are often built using API-based front-ends such as React, Angular, VueJs. In this session, we will dive into how Stitch can be used to provide APIs for your front-end, removing the need for writing and maintaining boilerplate back-end services, and having to manage and scale back-end servers. All to make developer’s lives easier.
In Atlas:
- Navigate to Stitch using the left hand navigation
- Click Create New Application
- In the dialog window, name the app, select one of your Atlas clusters to link it, and hit Create.
- Open your app by selecting it from the Stitch Applications list if it doesn’t automatically open after a few minutes
- On the Getting Started screen: Turn on Anonymous Authentication
- Click Review & Deploy Changes at the top and click Deploy in the dialog window
- Copy your Stitch App ID in top-left corner
- Go to https://stackblitz.com/edit/mongomart-world-session-1
- StackBlitz is an online IDE and app server for easily editing code and running apps online
- Edit config.js: change value of Stitch App ID on line 2 to your Stitch App ID
- Reload Live App (on the right) if you get following error: default app can only be set once; currently set to 'XXX’
- Your screen should look like this:
- Go to your Stitch app
- Go to Rules
- Create a new rule by clicking on the Add Collection button
- Enter
mongomart
as database name, and press return or click Create - Enter
item
as collection name, and press return or click Create - Select Users can only read all data as template
- Click the green Add collection button at the bottom
- Click Review & Deploy Changes at the top and click Deploy in the dialog window
- Enter
- Go back to the Live App and reload: do you see any products?
- Go back to the item-rule in Rules in your Stitch App
- Enable write-permissions by checking the Write checkboxes
- Edit the default (1) permissions and:
- Don’t forget to click Save on the top right and Review & Deploy Changes
- Go back to the Live App and:
- Go back to the item-rule in Rules in your Stitch App
- Disable the write-permissions by unchecking the Write checkboxes
- Edit the default permissions and:
- Uncheck Insert Documents and Delete Documents
- Click Done Editing
- Don’t forget to click Save on the top right and Review & Deploy Changes
- All set now!
- Go back to Rules in your Stitch App
- Create a new rule by clicking on … and Add Database/Collection
- Enter
mongomart
as database name - Enter
reviews
as collection name, and press return or click Create - Select Users can read all data, but only write their own data as template
- Enter
userid
for field name for User ID - Click the green Add collection button at the bottom
- Review & Deploy Changes
- Enter
- Go back to Live App, reload, and:
We’re going to display recent reviews for the product detail page.
- Edit in same file:
- Add following code to fetchReviews() on line 25:
- Get database handle:
const db = this.props.client .getServiceClient( RemoteMongoClient.factory, stitchClusterNames.reviews ) .db(dbName);
- Query database:
this.props.clientAuthenticated .then(() => db .collection(collNames.reviews) .find({productId: this.props.itemId}) .asArray() )
- Process response:
.then(response => { if (response) { this.setState({ reviews: response, reviewsError: null }); } })
- Error handling:
.catch(err => { this.setState({ reviewsError: err }); console.error(err); });
- Get database handle:
- Add following code to fetchReviews() on line 25:
- Test & Review: Go to Live App, reload, go to one of the product pages and try submitting a review.
- Go to Functions in your Stitch App
- Click Create New Function
- Go back to Live app, reload, and:
- Go back to Rules in your Stitch App
- Create a new rule by clicking on … and Add Database/Collection
- Enter
mongomart
as database name - Enter
users
as collection name, and press return or click Create - Select Users can only read and write their own data as template
- Enter
_id
for field name for User ID - Click the green Add collection button at the bottom
- Review & Deploy Changes
- Enter
- Go back to Live App, reload, and: