Book Image

Node.js By Example

Book Image

Node.js By Example

Overview of this book

Table of Contents (18 chapters)
Node.js By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Selecting friends and sending their IDs to the backend


We will start with the tagging of not only random users but also the friends of the current user. The functionality that we want to build will be placed on the home page. The form that creates a new post will contain a list of checkboxes. The very first step will be to fetch the friends from the API. In Chapter 6, Adding Friendship Capabilities, we already did that. We have a models/Friends.js file that queries the Node.js server and returns a list of users. So, let's use it. At the top of controllers/Home.js, we will add the following:

var Friends = require('../models/Friends');

Later, in the onrender handler, we will use the required module. The result of the API will be set as a value to a local friends variable in the following way:

var friends = new Friends();
friends.fetch(function(err, result) {
  if (err) { throw err; }
  self.set('friends', result.friends);
});

The controller has the user's friends in its data structure, and we...