Skip to content

Commit 0e1015b

Browse files
Merge pull request firebase#2 from firebase/jw-error-handlers
Added error handling and fixed some typos
2 parents bb55b2d + 2a479b5 commit 0e1015b

File tree

5 files changed

+31
-21
lines changed

5 files changed

+31
-21
lines changed

CONTRIBUTING.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ We'd love for you to contribute to our source code and to make the Firebase Node
1010
- [Coding Rules](#rules)
1111
- [Signing the CLA](#cla)
1212

13-
## <a name="coc"></a> Code of Conduct
13+
## <a name="coc"></a>Code of Conduct
1414

1515
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.
1616

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

2323
If you are subject to or witness unacceptable behavior, or have any other concerns, please drop us a line at nivco@google.com.
2424

25-
## <a name="question"></a> Got a Question or Problem?
25+
## <a name="question"></a>Got a Question or Problem?
2626

2727
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.
2828

@@ -37,14 +37,14 @@ Where have you looked?
3737
Where did you expect to find this information?
3838
```
3939

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

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

47-
## <a name="submit"></a> Submission Guidelines
47+
## <a name="submit"></a>Submission Guidelines
4848

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

164-
## <a name="rules"></a> Coding Rules
164+
## <a name="rules"></a>Coding Rules
165165

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

168-
## <a name="cla"></a> Signing the CLA
168+
## <a name="cla"></a>Signing the CLA
169169

170170
Please sign our [Contributor License Agreement][google-cla] (CLA) before sending pull requests. For any code
171171
changes to be accepted, the CLA must be signed. It's a quick process, we promise!

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# Firebase Quickstarts for Node.JS
1+
# Firebase Quickstarts for Node.js
22

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

55
## How to make contributions?
66
Please read and follow the steps in the [CONTRIBUTING.md](CONTRIBUTING.md)

database/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
Firebase Node Realtime Database Quickstart
1+
Firebase Node.js Realtime Database Quickstart
22
==========================================
33

4-
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.
4+
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.
55

66
This server will:
77
- Update the star counts for all posts.
@@ -17,12 +17,12 @@ Getting Started
1717
---------------
1818

1919
- Create your project on the [Firebase Console](https://console.firebase.google.com).
20-
- 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.
21-
- Change the `<PROJECT_ID>` and `<PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE>` placeholders in `index.js`.
22-
- Configure your email transport in `index.js`.
20+
- 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.
21+
- Change the `<PROJECT_ID>` and `<PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE>` placeholders in [`index.js`](index.js).
22+
- Configure your email transport in [`index.js`](index.js).
2323
- Run `npm install`.
24-
- Run `node index.js` to run the node app locally.
25-
- 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.
24+
- Run `node index.js` to run the Node.js app locally.
25+
- 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.
2626

2727
Support
2828
-------

database/index.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var escape = require('escape-html');
2929
// For other types of transports (Amazon SES, Sendgrid...) see https://nodemailer.com/2-0-0-beta/setup-transporter/
3030
var mailTransport = nodemailer.createTransport('smtps://<user>%40gmail.com:<password>@smtp.gmail.com');
3131

32-
// TODO(DEVELOPER): Create your Change the 2 placeholders below.
32+
// TODO(DEVELOPER): Change the two placeholders below.
3333
// [START initialize]
3434
// Initialize the app with a service account, granting admin privileges
3535
firebase.initializeApp({
@@ -63,6 +63,8 @@ function sendNotificationToUser(uid, postId) {
6363
});
6464
}
6565
// [END_EXCLUDE]
66+
}).catch(function(error) {
67+
console.log('Failed to send notification to user:', error);
6668
});
6769
}
6870
// [END single_value_read]
@@ -101,7 +103,7 @@ function updateStarCount(postRef) {
101103
* Keep the likes count updated and send email notifications for new likes.
102104
*/
103105
function startListeners() {
104-
firebase.database().ref('/posts').on('child_added', function (postSnapshot) {
106+
firebase.database().ref('/posts').on('child_added', function(postSnapshot) {
105107
var postReference = postSnapshot.ref;
106108
var uid = postSnapshot.val().uid;
107109
var postId = postSnapshot.key;
@@ -112,12 +114,16 @@ function startListeners() {
112114
// [START_EXCLUDE]
113115
updateStarCount(firebase.database().ref('user-posts/' + uid + '/' + postId));
114116
// [END_EXCLUDE]
117+
}, function(error) {
118+
console.log('Failed to add "value" listener at /posts/' + postId + '/stars node:', error);
115119
});
116120
// [END post_value_event_listener]
117121
// Send email to author when a new star is received.
118122
// [START child_event_listener_recycler]
119123
postReference.child('stars').on('child_added', function(dataSnapshot) {
120124
sendNotificationToUser(uid, postId);
125+
}, function(error) {
126+
console.log('Failed to add "child_added" listener at /posts/' + postId + '/stars node:', error);
121127
});
122128
// [END child_event_listener_recycler]
123129
});
@@ -141,13 +147,15 @@ function startWeeklyTopPostEmailer() {
141147
var allUsers = resp[1].val();
142148
var emailText = createWeeklyTopPostsEmailHtml(topPosts);
143149
sendWeeklyTopPostEmail(allUsers, emailText);
150+
}).catch(function(error) {
151+
console.log('Failed to start weekly top posts emailer:', error);
144152
});
145153
});
146154
console.log('Weekly top posts emailer started...');
147155
}
148156

149157
/**
150-
* Sends the Weekly top post email to all users in the given `users` object.
158+
* Sends the weekly top post email to all users in the given `users` object.
151159
*/
152160
function sendWeeklyTopPostEmail(users, emailHtml) {
153161
Object.keys(users).forEach(function(uid) {
@@ -163,16 +171,18 @@ function sendWeeklyTopPostEmail(users, emailHtml) {
163171
console.log('Weekly top posts email sent to: ' + user.email);
164172
// Save the date at which we sent the weekly email.
165173
// [START basic_write]
166-
firebase.database().child('/users/' + uid + '/lastSentWeeklyTimestamp')
174+
return firebase.database().child('/users/' + uid + '/lastSentWeeklyTimestamp')
167175
.set(firebase.database.ServerValue.TIMESTAMP);
168176
// [END basic_write]
177+
}).catch(function(error) {
178+
console.log('Failed to send weekly top posts email:', error);
169179
});
170180
}
171181
});
172182
}
173183

174184
/**
175-
* Creates the text for the Weekly top posts email given an Object of top posts.
185+
* Creates the text for the weekly top posts email given an Object of top posts.
176186
*/
177187
function createWeeklyTopPostsEmailHtml(topPosts) {
178188
var emailHtml = '<h1>Here are this week\'s top posts:</h1>';

database/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "firebase-database-quickstart",
33
"version": "1.0.0",
4-
"description": "Firebase Node Database Quickstart Sample",
4+
"description": "Firebase Node.js Database Quickstart Sample",
55
"main": "index.js",
66
"author": "nivco@google.com",
77
"license": "Apache License Version 2.0",

0 commit comments

Comments
 (0)