使用async.js深度填充sails.js [英] Using async.js for deep populating sails.js

查看:70
本文介绍了使用async.js深度填充sails.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sails.js(v12)中的函数存在很大问题。我正在尝试使用async(v2.3)获取所有userDetail以深度填充我的用户信息:

I have a big issue with my function in sails.js (v12). I'm trying to get all userDetail using async (v2.3) for deep populating my user info:

 userDetail: function (req, res) {
    var currentUserID = authToken.getUserIDFromToken(req);
    async.auto({
        //Find the User
        user: function (cb) {
            User
                .findOne({ id: req.params.id })
                .populate('userFollowing')
                .populate('userFollower')
                .populate('trips', { sort: 'createdAt DESC' })
                .exec(function (err, foundedUser) {
                    if (err) {
                        return res.negotiate(err);
                    }
                    if (!foundedUser) {
                        return res.badRequest();
                    }
                    // console.log('foundedUser :', foundedUser);
                    cb(null, foundedUser);
                });
        },
        //Find me
        me: function (cb) {
            User
                .findOne({ id: currentUserID })
                .populate('myLikedTrips')
                .populate('userFollowing')
                .exec(function (err, user) {
                    var likedTripIDs = _.pluck(user.myLikedTrips, 'id');
                    var followingUserIDs = _.pluck(user.userFollowing, 'id');
                    cb(null, { likedTripIDs, followingUserIDs });
                });
        },

        populatedTrip: ['user', function (results, cb) {
            Trip.find({ id: _.pluck(results.user.trips, 'id') })
                .populate('comments')
                .populate('likes')
                .exec(function (err, tripsResults) {
                    if (err) {
                        return res.negotiate(err);
                    }
                    if (!tripsResults) {
                        return res.badRequest();
                    }
                    cb(null, _.indexBy(tripsResults, 'id'));
                });
        }],

        isLiked: ['populatedTrip', 'me', 'user', function (results, cb) {
            var me = results.me;
            async.map(results.user.trips, function (trip, callback) {
                trip = results.populatedTrip[trip.id];

                if (_.contains(me.likedTripIDs, trip.id)) {
                    trip.hasLiked = true;
                } else {
                    trip.hasLiked = false;
                }

                callback(null, trip);
            }, function (err, isLikedTrip) {
                if (err) {
                    return res.negotiate(err);
                }
                cb(null, isLikedTrip);
            });
        }]
    },

        function finish(err, data) {
            if (err) {
                console.log('err = ', err);
                return res.serverError(err);
            }

            var userFinal = data.user;
            //userFinal.trips = data.isLiked;
            userFinal.trips = "test";
            return res.json(userFinal);
        }
    );
},

我几乎试图得到这个修复,但没有任何工作......
我能够获得我的旅行数组(data.isLiked),但我无法获得userFInal旅行。

I tried almost everthing to get this fix but nothing is working... I am able to get my array of trips(data.isLiked) but I couldn't get my userFInal trips.

我尝试设置字符串值userFinal.trips:

I try to set string value on the userFinal.trips:

 {
  "trips": [], // <-- my pb is here !! 
  "userFollower": [
    {
      "user": "5777fce1eeef472a1d69bafb",
      "follower": "57e44a8997974abc646b29ca",
      "id": "57efa5cf605b94666aca0f11"
    }
  ],
  "userFollowing": [
    {
      "user": "57e44a8997974abc646b29ca",
      "follower": "5777fce1eeef472a1d69bafb",
      "id": "5882099b9c0c9543706d74f6"
    }
  ],
  "email": "test2@test.com",
  "userName": "dany",
  "isPrivate": false,
  "bio": "Hello",
  "id": "5777fce1eeef472a1d69bafb"
}



问题



如何将我的行程数组(isLiked)粘贴到我的用户行程数组中?
为什么我的结果不是我期望的结果?

Question

How should I do to get my array of trips (isLiked) paste to my user trips array? Why my results is not what I'm expecting to have?

感谢您的回答。

推荐答案

在覆盖模型中的任何关联之前使用 .toJSON()

否则默认 toJSON 实现会覆盖对模型关联数据所做的任何更改。

Use .toJSON() before overwriting any association in model.
Otherwise default toJSON implementation overrides any changes made to model associated data.

var userFinal = data.user.toJSON(); // Use of toJSON
userFinal.trips = data.isLiked;
return res.json(userFinal);






另一方面,请使用JS .map _。map 代替 async.map 因为有内部函数不是异步操作。否则你可能面临 RangeError:超出最大调用堆栈大小问题。


On another note, use JS .map or _.map in place of async.map as there is not asynchronous operation in inside function. Otherwise you may face RangeError: Maximum call stack size exceeded issue.

此外,返回任何内容可能更好仅来自最终回调的回复。 (从 async.auto res.negotiate res.badRequest c>的第一个论点)。它允许制作响应方法终端

Also, it might be better to return any response from final callback only. (Remove res.negotiate, res.badRequest from async.auto's first argument). It allows to make response method terminal

这篇关于使用async.js深度填充sails.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆