Skip to content

Commit

Permalink
Added error handling and fixed some typos
Browse files Browse the repository at this point in the history
  • Loading branch information
jwngr committed Jun 6, 2016
1 parent bb55b2d commit 2a479b5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ We'd love for you to contribute to our source code and to make the Firebase Node
- [Coding Rules](#rules)
- [Signing the CLA](#cla)

## <a name="coc"></a> Code of Conduct
## <a name="coc"></a>Code of Conduct

As contributors and maintainers of the Firebase Node.JS Quickstarts project, we pledge to respect everyone who contributes by posting issues, updating documentation, submitting pull requests, providing feedback in comments, and any other activities.

Expand All @@ -22,7 +22,7 @@ If any member of the community violates this code of conduct, the maintainers of

If you are subject to or witness unacceptable behavior, or have any other concerns, please drop us a line at [email protected].

## <a name="question"></a> Got a Question or Problem?
## <a name="question"></a>Got a Question or Problem?

If you have questions about how to use the Firebase Node.JS Quickstarts, please direct these to [StackOverflow][stackoverflow] and use the `firebase` tag. We are also available on GitHub issues.

Expand All @@ -37,14 +37,14 @@ Where have you looked?
Where did you expect to find this information?
```

## <a name="issue"></a> Found an Issue?
## <a name="issue"></a>Found an Issue?
If you find a bug in the source code or a mistake in the documentation, you can help us by
submitting an issue to our [GitHub Repository][github]. Even better you can submit a Pull Request
with a fix.

See [below](#submit) for some guidelines.

## <a name="submit"></a> Submission Guidelines
## <a name="submit"></a>Submission Guidelines

### Submitting an Issue
Before you submit your issue search the archive, maybe your question was already answered.
Expand Down Expand Up @@ -161,11 +161,11 @@ from the main (upstream) repository:
git pull --ff upstream master
```

## <a name="rules"></a> Coding Rules
## <a name="rules"></a>Coding Rules

We generally follow the [Google JavaScript style guide][js-style-guide].

## <a name="cla"></a> Signing the CLA
## <a name="cla"></a>Signing the CLA

Please sign our [Contributor License Agreement][google-cla] (CLA) before sending pull requests. For any code
changes to be accepted, the CLA must be signed. It's a quick process, we promise!
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Firebase Quickstarts for Node.JS
# Firebase Quickstarts for Node.js

A collection of quickstart samples demonstrating the Firebase APIs using the Node.JS SDK. For more information, see https://firebase.google.com.
A collection of quickstart samples demonstrating the Firebase APIs using the Node.js SDK. For more information, see https://firebase.google.com.

## How to make contributions?
Please read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md)
Expand Down
14 changes: 7 additions & 7 deletions database/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Firebase Node Realtime Database Quickstart
Firebase Node.js Realtime Database Quickstart
==========================================

The Node Firebase Database quickstart demonstrates how to connect to and use the Firebase Realtime Database using Node through a simple social blogging app. It will interoperate with the Web, iOS and Android database quickstarts.
The Node.js Firebase Database quickstart demonstrates how to connect to and use the Firebase Realtime Database using Node.js through a simple social blogging app. It will interoperate with the Web, iOS and Android database quickstarts.

This server will:
- Update the star counts for all posts.
Expand All @@ -17,12 +17,12 @@ Getting Started
---------------

- Create your project on the [Firebase Console](https://console.firebase.google.com).
- Create a Service account as described in [Adding Firebase to your Server](https://firebase.google.com/docs/server/setup) and drop the file in this directory.
- Change the `<PROJECT_ID>` and `<PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE>` placeholders in `index.js`.
- Configure your email transport in `index.js`.
- Create a service account as described in [Adding Firebase to your Server](https://firebase.google.com/docs/server/setup) and drop the file in this directory. Or use the provided test service account.
- Change the `<PROJECT_ID>` and `<PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE>` placeholders in [`index.js`](index.js).
- Configure your email transport in [`index.js`](index.js).
- Run `npm install`.
- Run `node index.js` to run the node app locally.
- Configure and run one of the Database quickstarts for [Web](https://github.com/firebase/quickstart-js/tree/master/database), [iOS](https://github.com/firebase/quickstart-js/tree/master/database) or [Android](https://github.com/firebase/quickstart-android/tree/master/database). Then use one of these apps to publish new posts: you should receive email notifications when one of your posts have received a new star and the starred counter should be ept up to date by the app.
- Run `node index.js` to run the Node.js app locally.
- Configure and run one of the Database quickstarts for [Web](https://github.com/firebase/quickstart-js/tree/master/database), [iOS](https://github.com/firebase/quickstart-ios/tree/master/database) or [Android](https://github.com/firebase/quickstart-android/tree/master/database). Then use one of these apps to publish new posts: you should receive email notifications when one of your posts have received a new star and the starred counter should be kept up to date by the app.

Support
-------
Expand Down
20 changes: 15 additions & 5 deletions database/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var escape = require('escape-html');
// For other types of transports (Amazon SES, Sendgrid...) see https://nodemailer.com/2-0-0-beta/setup-transporter/
var mailTransport = nodemailer.createTransport('smtps://<user>%40gmail.com:<password>@smtp.gmail.com');

// TODO(DEVELOPER): Create your Change the 2 placeholders below.
// TODO(DEVELOPER): Change the two placeholders below.
// [START initialize]
// Initialize the app with a service account, granting admin privileges
firebase.initializeApp({
Expand Down Expand Up @@ -63,6 +63,8 @@ function sendNotificationToUser(uid, postId) {
});
}
// [END_EXCLUDE]
}).catch(function(error) {
console.log('Failed to send notification to user:', error);
});
}
// [END single_value_read]
Expand Down Expand Up @@ -101,7 +103,7 @@ function updateStarCount(postRef) {
* Keep the likes count updated and send email notifications for new likes.
*/
function startListeners() {
firebase.database().ref('/posts').on('child_added', function (postSnapshot) {
firebase.database().ref('/posts').on('child_added', function(postSnapshot) {
var postReference = postSnapshot.ref;
var uid = postSnapshot.val().uid;
var postId = postSnapshot.key;
Expand All @@ -112,12 +114,16 @@ function startListeners() {
// [START_EXCLUDE]
updateStarCount(firebase.database().ref('user-posts/' + uid + '/' + postId));
// [END_EXCLUDE]
}, function(error) {
console.log('Failed to add "value" listener at /posts/' + postId + '/stars node:', error);
});
// [END post_value_event_listener]
// Send email to author when a new star is received.
// [START child_event_listener_recycler]
postReference.child('stars').on('child_added', function(dataSnapshot) {
sendNotificationToUser(uid, postId);
}, function(error) {
console.log('Failed to add "child_added" listener at /posts/' + postId + '/stars node:', error);
});
// [END child_event_listener_recycler]
});
Expand All @@ -141,13 +147,15 @@ function startWeeklyTopPostEmailer() {
var allUsers = resp[1].val();
var emailText = createWeeklyTopPostsEmailHtml(topPosts);
sendWeeklyTopPostEmail(allUsers, emailText);
}).catch(function(error) {
console.log('Failed to start weekly top posts emailer:', error);
});
});
console.log('Weekly top posts emailer started...');
}

/**
* Sends the Weekly top post email to all users in the given `users` object.
* Sends the weekly top post email to all users in the given `users` object.
*/
function sendWeeklyTopPostEmail(users, emailHtml) {
Object.keys(users).forEach(function(uid) {
Expand All @@ -163,16 +171,18 @@ function sendWeeklyTopPostEmail(users, emailHtml) {
console.log('Weekly top posts email sent to: ' + user.email);
// Save the date at which we sent the weekly email.
// [START basic_write]
firebase.database().child('/users/' + uid + '/lastSentWeeklyTimestamp')
return firebase.database().child('/users/' + uid + '/lastSentWeeklyTimestamp')
.set(firebase.database.ServerValue.TIMESTAMP);
// [END basic_write]
}).catch(function(error) {
console.log('Failed to send weekly top posts email:', error);
});
}
});
}

/**
* Creates the text for the Weekly top posts email given an Object of top posts.
* Creates the text for the weekly top posts email given an Object of top posts.
*/
function createWeeklyTopPostsEmailHtml(topPosts) {
var emailHtml = '<h1>Here are this week\'s top posts:</h1>';
Expand Down
2 changes: 1 addition & 1 deletion database/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "firebase-database-quickstart",
"version": "1.0.0",
"description": "Firebase Node Database Quickstart Sample",
"description": "Firebase Node.js Database Quickstart Sample",
"main": "index.js",
"author": "[email protected]",
"license": "Apache License Version 2.0",
Expand Down

0 comments on commit 2a479b5

Please sign in to comment.