Wednesday, 26 April 2017

How to only show specific results for a user using drywall-angular in the Mean Stack?

I’ve been having some problems during the development of our application using the MEAN stack. I’ve been using drywall-angular which is available on GitHub here.

I’ve been adapting the code to our needs. I don’t have much experience using the MEAN stack. The first issue that I’m having is with a results table. I want to only show results for the account signed in. For example, if account1 is signed in into the application, account1 should only see users that they have created. However, at the moment, all accounts can see all users regardless of which one has signed in. On my user schema, I have a userCreated object which will store the objectID of the account that created the user. This is working fine. The only issue that I’m having is on the index.js page for showing the users in the table. I want to do a check for the object id of the account signed in and the object id of the userCreated on the user. If this is equal, then show the items that have matched. I’ve tried an if statement but this didn’t work. Our account schema has the following code:

userCreated: {
      id: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
      name: { type: String, default: '' },
      time: { type: Date, default: Date.now }
    }

I believe if I add the code for the check here (our index.js) on the filter for the table, it could work:

exports.find = function(req, res, next){
  req.query.username = req.query.username ? req.query.username : '';
  req.query.limit = req.query.limit ? parseInt(req.query.limit, null) : 20;
  req.query.page = req.query.page ? parseInt(req.query.page, null) : 1;
  req.query.sort = req.query.sort ? req.query.sort : '_id';

  var filters = {};
  var outcome = {};

    var getAccountData = function(callback) {
    req.app.db.models.Account.find(req.user.roles.account.id, 'user.id').exec(function(err, account) {
      if (err) {
        return callback(err, null);
      }

      outcome.account = account;
      callback(null, 'done');
    });
  };

    var getUserData = function(callback) {
    req.app.db.models.User.find(req.user._id, 'userCreated.id').exec(function(err, user) {
      if (err) {
        return callback(err, null);
      }

      outcome.user = user;
      callback(null, 'done');
    });
  };

if (outcome.user == outcome.account) {
    filters['userCreated.id'] = { $exists: true};
  }

  req.app.db.models.User.pagedFind({
    filters: filters,
    keys: 'username email isActive userCreated',
    limit: req.query.limit,
    page: req.query.page,
    sort: req.query.sort

I believe this will solve the problem but unfortunately, I can’t figure out why the check isn't working?

The second problem that I’m having is with Jade. With drywall-angular, the text box works fine but I’m having issues with a dropdown menu. I’m able to create an item on my index.js page using a dropdown menu. After the item has been created, I’m redirected to details.js where the item should be selected already in the dropdown menu. I’m not sure how to design this in Jade as I’m dealing with a new object in the code called “errfor”, which was already implemented in the initial code.

  div.control-group(class!='<%= errfor.dropdown ? "has-error" : "" %>')
    label.control-label Items:
      select.form-control(name='dropdown')
        option(value='bike') bike
        option(value='guitar') guitar
  span.help-block <%= errfor.dropdown %> 
  div.control-group(class!='<%= errfor.name ? "has-error" : "" %>')
    label.control-label Name:
    input.form-control(type='text', name='name', value!='<%- name %>')
    span.help-block <%= errfor.name %>



via mfitzpa

No comments:

Post a Comment