Mongoose: Get full list of users

前端 未结 8 1114
遇见更好的自我
遇见更好的自我 2020-12-12 12:54

I have tried to use Mongoose to send the list of all users as follows:

server.get(\'/usersList\', function(req, res) {
    var users = {};

    User.find({},         


        
相关标签:
8条回答
  • 2020-12-12 13:16

    Same can be done with async await and arrow function

    server.get('/usersList', async (req, res) => {
    
    const users = await User.find({});
    
    const userMap = {};
    users.forEach((user) => {
        userMap[user._id] = user;
    });
    
    res.send(userMap);
    
    });
    
    0 讨论(0)
  • 2020-12-12 13:21

    My Solution

    User.find()
            .exec()
            .then(users => {
                const response = {
                    count: users.length,
                    users: users.map(user => {
    
                        return {
                            _id: user._id,
                            // other property
                        }
    
                    })
    
                };
                res.status(200).json(response);
            }).catch(err => {
            console.log(err);
            res.status(500).json({
                success: false
            })
        })
    
    0 讨论(0)
  • 2020-12-12 13:32

    If you'd like to send the data to a view pass the following in.

        server.get('/usersList', function(req, res) {
            User.find({}, function(err, users) {
               res.render('/usersList', {users: users});
            });
        });
    

    Inside your view you can loop through the data using the variable users

    0 讨论(0)
  • 2020-12-12 13:32

    To make function to wait for list to be fetched.

    getArrayOfData() {
        return DataModel.find({}).then(function (storedDataArray) {
            return storedDataArray;
        }).catch(function(err){
            if (err) {
                throw new Error(err.message);
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-12 13:33

    Well, if you really want to return a mapping from _id to user, you could always do:

    server.get('/usersList', function(req, res) {
      User.find({}, function(err, users) {
        var userMap = {};
    
        users.forEach(function(user) {
          userMap[user._id] = user;
        });
    
        res.send(userMap);  
      });
    });
    

    find() returns all matching documents in an array, so your last code snipped sends that array to the client.

    0 讨论(0)
  • 2020-12-12 13:33

    In case we want to list all documents in Mongoose collection after update or delete

    We can edit the function to some thing like this:

    exports.product_update = function (req, res, next) {
            Product.findByIdAndUpdate(req.params.id, {$set: req.body}, function (err, product) {
                if (err) return next(err);
                Product.find({}).then(function (products) {
                    res.send(products);
                    });
                //res.send('Product udpated.');
            });
        };
    

    This will list all documents on success instead of just showing success message

    0 讨论(0)
提交回复
热议问题