Home Ask Login Register

Developers Planet

Your answer is one click away!

Drasius February 2016

In project created with "generator-angular-fullstack", how to filter list items (in server side)?

I am using generator-angular-fullstack:

npm install -g generator-Angular-Fullstack@3.2.0

Created project with settings: Out of the box I create an AngularJS app with an Express server.


? What would you like to write markup with? HTML ? What would you like to write stylesheets with? CSS ? What Angular router would you like to use? uiRouter ? Would you like to include Bootstrap? Yes ? Would you like to include UI Bootstrap? Yes


? What would you like to use for data modeling? Mongoose (MongoDB) ? Would you scaffold out an authentication boilerplate? Yes ? Would you like to include additional oAuth strategies? Facebook, Twitter ? Would you like to use socket.io? Yes


? What would you like to write tests with? (Use arrow keys)? Jasmine

I created new endpoint "Reservations":

yo angular-fullstack:endpoint reservation       //Server side
yo angular-fullstack:route reservatios //Client Route

Updated that new page were populated "reservatios" list. But it gets all "Reservation" items.

I want to get all reservations when user isAdmin. In other cases (non users, or simple users) I want to get only active Reservations.

So there is simple Reservation model:

var ReservationSchema = new Schema({
   date: {
    type: Date,
    required: true
  //,user: {
  //  type : Schema.ObjectId,
  //  ref : 'User'

Reservations is active, when the "new Date()" < reservatio.date.

So there is these server side controllers: server\api\reservation\ reservation.controller.js


Drasius February 2016

Finally find out how to do filtering :)...

In example this was my model (server\api\reservation\reservation.model.js):

var mongoose = require('bluebird').promisifyAll(require('mongoose'));
var Schema = mongoose.Schema;
var ReservationSchema = new Schema({
  date: {
    type: Date,
    required: true

export default mongoose.model('Reservation', ReservationSchema);

I added new static function to it:

ReservationSchema.statics.findActive = function (next) {
    return this.find({"date": {"$gte": new Date()}});

Then added the new function to reservation controller (server\api\reservation\reservation.controller.js)

// Gets a list of Active Reservations
export function active(req, res) {

Finally added route for active reservations (server\api\reservation\index.js):

router.get('/active', controller.active);

Now it works :). Check by calling in browser:

http://localhost:9000/api/reservations // Gets all reservations http://localhost:9000/api/reservations/active // Gets only active reservations - with is "reservation.date > new Date()".

Post Status

Asked in February 2016
Viewed 1,727 times
Voted 4
Answered 1 times


Leave an answer

Quote of the day: live life