0

I'm trying to do is display a list of user display names on one of my webpages by doing a simple http get request.

In my users.server.controller.js file I have this function:

exports.list = function(req, res) { 
    User.find().populate('displayName').exec(function(err, users) {
        console.log(users);
        if (err) {
            return res.status(400).send({
                message: errorHandler.getErrorMessage(err)
            });
        } else {
            res.jsonp(users);
        }
    });
};

When I call this function with an $http.get() request from AngularJS controller I get a 400 error.

Here is my Mongoose User schema

/**
 * User Schema
 */
var UserSchema = new Schema({
    firstName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your first name']
    },
    lastName: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your last name']
    },
    organization: {
        type: String,
        trim: true,
        default: '',
        required: 'Please fill in an organization name'
    },
    position: {
        type: String,
        trim: true,
        default: '',
        required: 'Please fill in the title of your position'
    },
    displayName: {
        type: String,
        trim: true
    },
    email: {
        type: String,
        trim: true,
        default: '',
        validate: [validateLocalStrategyProperty, 'Please fill in your email'],
        match: [/.+\@.+\..+/, 'Please fill a valid email address']
    },
    username: {
        type: String,
        unique: 'testing error message',
        required: 'Please fill in a username',
        trim: true
    },
    password: {
        type: String,
        default: '',
        validate: [validateLocalStrategyPassword, 'Password should be longer']
    },
    salt: {
        type: String
    },
    provider: {
        type: String,
        required: 'Provider is required'
    },
    providerData: {},
    additionalProvidersData: {},
    roles: {
        type: [{
            type: String,
            enum: ['user', 'admin']
        }],
        default: ['user']
    },
    updated: {
        type: Date
    },
    created: {
        type: Date,
        default: Date.now
    },
    /* For reset password */
    resetPasswordToken: {
        type: String
    },
    resetPasswordExpires: {
        type: Date
    }
});

1 Answer 1

1

It seems that you confuse between populate and select. populate is to be used when you have a reference to an external schema like owner : { type: Schema.ObjectId, ref: 'User' }. If you want to display owner fields, then you need to call populate('owner') to load the user. What you need is to call .select("displayName") in order to retrieve only the displayName in your result.

Sign up to request clarification or add additional context in comments.

1 Comment

could you mark my answer as the good one ? (need to upgrade my score...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.