0

This is my codes. i got result when i was using find() all fields but i want to use find with where condition that produce error. what should i change ??

index : function(req, res, next) {
   User.find({}).where({userlevel:{'=' : 'admin'}}).exec(function (err, usernames) {
        if (err) { 
            res.send(400);
        } else {

            res.view({
                usernames : usernames

            });
        }

    },

i want to get all admin usernames from mongoDB..

in html

<% _.each(usernames,function(err,user) { %>

            <%= user.usernames %>

        <% }) %>

Error :

usernames not defined..

pls help me..I am new to Sails

Aravinth
  • 397
  • 1
  • 4
  • 19

1 Answers1

0

Try this:

index : function(req, res) {
   User.find().where({userlevel: 'admin'}).exec(function (err, users) {
        if (err) { 
            res.send(400);
        } else {
            res.view({
                usernames: users
            });
        }

    }

Note that users is a list of users, not just usernames.

In your view:

<% _.each(usernames, function(user) { %>
    <%= user.username %>
<% }) %>
roign
  • 452
  • 5
  • 10
  • I updated the answer. Can you share the model code with us as well? The code in `api/models/User.js`. – roign Feb 04 '15 at 09:06
  • attributes: { username: { type: 'string' },............ userlevel: { type: 'string' }, } – Aravinth Feb 04 '15 at 14:52
  • So it seems that it was a typo. In your view it should be `user.username` (like in my answer), not `user.usernames`. – roign Feb 04 '15 at 15:41
  • hi..sorry ..attributes: { usernames: { type: 'string' },............ userlevel: { type: 'string' }, } .this is correct – Aravinth Feb 05 '15 at 09:33
  • Could you change in the controller `res.view({usernames: users})` to `res.view({users: users})`? Because you are using `username` in both controller and view, so it's ambiguous. After you change this you should check if the error stays the same: `usernames not defined` – roign Feb 05 '15 at 09:50