我将获得答案数组
但如何获取已回答的用户信息;
流程为获取帖子列表,发布用户信息,用户关注者数量,发帖数量,登录用户喜欢帖子对错, 答案,已回答的用户信息和已回答的用户关注者数
exports.GetPostList = function(req, res) {
var SkipCoun = 0;
SkipCoun = parseInt(req.params.Limit) * 10;
QuestionsPostModel.QuestionsPostType.find({}, {} , {sort:{createdAt : -1}, skip: SkipCoun, limit: 10 }, function(err, result) {
if(err) {
res.status(500).send({status:"False", message: "Some error occurred while Find Following Users ."});
} else {
const GetUserData = (result) =>
Promise.all(
result.map(info => getPostInfo(info))
).then(
results => {
let [UserInfo, UserFollowers, PostRatingCount, UserRatedCount, AnswersCount, AswersArray ] =
results.reduce(([allOne, allTwo, allThree, allFour, allFive, allSix ], [one, two, three, four, five, six]) =>
[allOne.concat([one]), allTwo.concat([two]), allThree.concat([three]), allFour.concat([four]), allFive.concat([five]), allSix.concat([six])], [ [], [], [], [], [], [] ]);
res.send({ status: "True", UserInfo: UserInfo, UserFollowers: UserFollowers, PostRatingCount: PostRatingCount, UserRatedCount: UserRatedCount, AnswersCount:AnswersCount, AswersArray:AswersArray })
}
).catch(err => res.send({ status: "Fale",Error: err}) );
const getPostInfo = info =>
Promise.all([
UserModel.UserType.findOne({'_id': info.UserId }, usersProjection).exec(),
FollowModel.FollowUserType.count({'UserId': info.UserId}).exec(),
RatingModel.QuestionsRating.count({'PostId': info._id , 'ActiveStates':'Active' }).exec(),
RatingModel.QuestionsRating.count({'UserId': req.params.UserId, 'PostId': info._id, 'PostUserId': info.UserId, 'ActiveStates':'Active'}).exec(),
AnswerModel.QuestionsAnwer.count({'PostId': info._id , 'ActiveStates':'Active' }).exec(),
AnswerModel.QuestionsAnwer.find({ 'PostId':info._id }, 'AnswerText UserId Date' ).exec()
]);
GetUserData(result);
}
});
};
如何获得结果
答案 0 :(得分:1)
在您组建数组时,您将其传递到Promise.all()
内。
而你只需要数据。
Promise.all()
docs。
请参阅以下代码(我们可以做承诺.then()
和.catch()
):
exports.GetPostList = function (req, res) {
var SkipCoun = 0;
SkipCoun = parseInt(req.params.Limit) * 10;
QuestionsPostModel.QuestionsPostType.find({}, {}, { sort: { createdAt: -1 }, skip: SkipCoun, limit: 10 }, function (err, result) {
if (err) {
res.status(500).send({ status: "False", message: "Some error occurred while Find Following Users ." });
} else {
const GetUserData = (result) =>
Promise.all(
result.map(info => getPostInfo(info))
).then(
results => {
let [UserInfo, UserFollowers, PostRatingCount, UserRatedCount, AnswersCount, AswersArray] =
results.reduce(([allOne, allTwo, allThree, allFour, allFive, allSix], [one, two, three, four, five, six]) =>
[allOne.concat([one]), allTwo.concat([two]), allThree.concat([three]), allFour.concat([four]), allFive.concat([five]), allSix.concat([six])], [[], [], [], [], [], []]);
res.send({ status: "True", UserInfo: UserInfo, UserFollowers: UserFollowers, PostRatingCount: PostRatingCount, UserRatedCount: UserRatedCount, AnswersCount: AnswersCount, AswersArray: AswersArray })
}
).catch(err => res.send({ status: "Fale", Error: err }));
const getPostInfo = info =>
Promise.all([
UserModel.UserType.findOne({ '_id': info.UserId }, usersProjection).exec(),
FollowModel.FollowUserType.count({ 'UserId': info.UserId }).exec(),
RatingModel.QuestionsRating.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
RatingModel.QuestionsRating.count({ 'UserId': req.params.UserId, 'PostId': info._id, 'PostUserId': info.UserId, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.find({ 'PostId': info._id }, 'AnswerText UserId Date').exec()
]).then(data => {
let userData = data[0];
let followCount = data[1];
let ratingCount = data[2];
let ratingCountBy_postId = data[3];
let answerCount = data[4];
let answers = data[5];
// now you may need to construct some object or something and pass data as you required.
let result = {
user: userData,
follow_count: followCount,
rating_count: ratingCount,
rating_countBy_postId: ratingCountBy_postId,
answer_count: answerCount,
answers: answers
};
}).catch(error => {
console.log(error)
})
GetUserData(result);
}
});
};
答案 1 :(得分:0)
exports.GetPostList = function (req, res) {
var SkipCoun = 0;
SkipCoun = parseInt(req.params.Limit) * 10;
QuestionsPostModel.QuestionsPostType.find({}, {}, { sort: { createdAt: -1 }, skip: SkipCoun, limit: 10 }, function (err, result) {
if (err) {
res.status(500).send({ status: "False", message: "Some error occurred while Find Following Users ." });
} else {
const GetUserData = (result) =>
Promise.all(
result.map(info => getPostInfo(info))
).then( result =>{ console.log(result); res.send({ status: "True", data: result }) }
).catch(err => res.send({ status: "False", Error: err }));
const getPostInfo = info =>
Promise.all([
UserModel.UserType.findOne({ '_id': info.UserId }, usersProjection).exec(),
FollowModel.FollowUserType.count({ 'UserId': info.UserId }).exec(),
RatingModel.QuestionsRating.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
RatingModel.QuestionsRating.count({ 'UserId': req.params.UserId, 'PostId': info._id, 'PostUserId': info.UserId, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.count({ 'PostId': info._id, 'ActiveStates': 'Active' }).exec(),
AnswerModel.QuestionsAnwer.find({ 'PostId': info._id }, 'AnswerText UserId Date').exec()
]).then(data => {
let UserData = data[0];
let followCount = data[1];
let ratingCount = data[2];
let UserRating = data[3];
let AnswerCount = data[4];
let Answerdata = data[5];
var AnswersArray= new Array();
return GetAnsUserData();
async function GetAnsUserData(){
for (let ansInfo of Answerdata) {
await getAnswerInfo(ansInfo);
}
let result = {
_id: info._id,
UserId: UserData._id,
UserName: UserData.UserName,
UserCategoryId: UserData.UserCategoryId,
UserCategoryName: UserData.UserCategoryName,
UserImage: UserData.UserImage,
UserCompany: UserData.UserCompany,
UserProfession: UserData.UserProfession,
Followers:followCount,
PostTopicId: info.PostTopicId,
PostTopicName: info.PostTopicName,
PostDate: info.PostDate,
PostText: info.PostText ,
PostLink: info.PostLink,
PostImage: info.PostImage,
PostVideo: info.PostVideo,
RatingCount: ratingCount,
UserRating: UserRating,
AnswersCount: AnswerCount,
Answers: AnswersArray,
};
return result;
}
function getAnswerInfo(ansInfo){
return new Promise(( resolve, reject )=>{
UserModel.UserType.findOne({'_id': ansInfo.UserId }, usersProjection, function(err, AnsUserData) {
if(err) {
res.send({status:"Fale", Error:err });
reject(err);
} else {
FollowModel.FollowUserType.count({'UserId': AnsUserData._id}, function(newerr, count) {
if(newerr){
res.send({status:"Fale", Error:newerr });
reject(newerr);
}else{
var newArray = [];
newArray.push( {
_id: ansInfo._id,
UserId: AnsUserData._id,
UserName: AnsUserData.UserName,
UserCategoryId: AnsUserData.UserCategoryId,
UserCategoryName: AnsUserData.UserCategoryName,
UserImage: AnsUserData.UserImage,
UserCompany: AnsUserData.UserCompany,
UserProfession: AnsUserData.UserProfession,
Followers: count,
Date: ansInfo.Date,
PostId: ansInfo.PostId,
PostUserId: ansInfo.PostUserId ,
AnswerText: ansInfo.AnswerText
}
);
AnswersArray.push(newArray[0]);
resolve(newArray[0]);
}
});
}
});
});
}
}).catch(error => {
console.log(error)
})
GetUserData(result);
}
});
};
最后我得到答案 感谢所有