gpt4 book ai didi

javascript - JS - Express - Mongoose 在发送响应之前执行所有 Mongoose 的 promise

转载 作者:行者123 更新时间:2023-11-30 17:43:02 24 4
gpt4 key购买 nike

我正在做一个论坛 api,其中一个论坛有很多主题,主题有很多帖子,帖子可能有很多帖子。

关系是这样的:

var PostSchema = new Schema({
text: String,
authorId: String,
slug: Number,
posts: [{ type: Schema.Types.ObjectId, ref: 'Post'}],
created: { type: Date, default: Date.now }
});

父模型有一个子模型的 ID 列表。

我的 Controller 是这样的:

var util = require('util'),
mongoose = require('mongoose'),
Forum = mongoose.model('Forum'),
Thread = mongoose.model('Thread'),
Post = mongoose.model('Post'),
async = require('async');

exports.show = function(req, res){
var forums;

var getThreads = function(forum) {
return forum.populate('threads', function(err, _forum){
if(err) throw new Error(err);
forum.threads = _forum.threads;
forum.threads.forEach(getPosts);
return callback(err);
});
};

var getPosts = function(thread) {
return thread.populate('posts', function(err, _thread){
if(err) throw new Error(err);
thread.posts = _thread.posts;
thread.posts.forEach(getComments);
return callback(err);
});
};

var getComments = function(post) {
return post.populate('posts', function(err, _post){
if(err) throw new Error(err);
post.posts = _post.posts;
post.posts.forEach(getComments);
return callback(err);
});
};

async.parallel([
function(callback) {
return Forum.find({ ownerId: req.params.owner_id }).exec(function(err, _forums) {
if(err) throw new Error(err);
forums = _forums;
forums.forEach(getThreads);
return callback(err);
});
}
], function(err){
res.json(forums);
}
);

};

我需要制作完整的论坛对象,然后在响应中使用它,因为帖子有帖子我不能只做嵌套填充。

我尝试使用异步库,但它在 promise 之前执行回调函数。

如何构建完整的论坛对象?

最佳答案

您需要以异步方式正确处理您的树结构。试试这个方法:

(我还没有测试过,但希望它有效)

// ...
var Forum = mongoose.model('Forum');

exports.show = function(req, res){
//Get the owner's forums
Forum.find({ ownerId: req.params.owner_id }).exec(function(err, forums) {
if(err) throw new Error(err);
if(!forums.length) return response.json(forums); //Send an empty array if no forums where found

//Build forums one by one
var forum = forums.shift();
buildForum(forum, function () {
forum = forums.shift();
if (forum) {
buildForum(forum, this);
} else {
//All forums were built.
res.json(forums);
};
});
});

var buildForum = function (forum, onSuccess) {
forum.populate('threads', function(err, forum){
if(err) throw new Error(err);
if(!forum.threads.length) return onSuccess();

//Build threads one by one
var threads = forum.threads;
var thread = threads.shift();
buildThread(thread, function () {
thread = threads.shift();
if (thread) {
buildThread(thread, this);
} else {
//All threads were built.
onSuccess();
};
});
});
};

var buildThread = function (thread, onSuccess) {
thread.populate('posts', function(err, thread){
if(err) throw new Error(err);
if(!thread.posts.length) return onSuccess();

//Build posts one by one
var posts = thread.posts;
var post = posts.shift();
buildPost(post, function () {
post = posts.shift();
if (post) {
buildPost(post, this);
} else {
//All posts were built.
onSuccess();
};
});
});
};

var buildPost = function (post, onSuccess) {
post.populate('posts', function(err, post){
if(err) throw new Error(err);
if(!post.posts.length) return onSuccess();

//Build comments one by one
var posts = post.posts;
var _post = posts.shift();
buildPost(_post, function () {
_post = posts.shift();
if (_post) {
buildPost(_post, this);
} else {
//All comments were built.
onSuccess();
};
});
});
};
};

关于javascript - JS - Express - Mongoose 在发送响应之前执行所有 Mongoose 的 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20670873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com