gpt4 book ai didi

node.js - 无法找到一种简单的方法来摆脱每个 Node js 的多个异步(sails)

转载 作者:太空宇宙 更新时间:2023-11-04 00:40:31 25 4
gpt4 key购买 nike

所以这是交易:

我有一个对象数组和一个子对象数组

askedAdvices 
askedAdvice.replayAdvices

我正在通过父项循环,foreach通过子项循环,并且需要填充()两个对象(我正在使用 sails )

child 长得像:

askedAdvices = {
replayAdvices : [{
bookEnd : "<ID>",
user : "<ID>"
}]
}

所以我的目标是使用两个 findOne 查询循环并填充 bookEnd 和 user,但我对回调 hell 感到抓狂。这是模型代码:

询问建议模型

module.exports = {
schema : false,
attributes: {
bookStart : {
model : 'book'
},
replayAdvices : {
collection: 'replybookend'

},
user : {
model : 'user',
required : true
},
text : {
type : "text"
}
}
};

ReplyBookEnd模型

module.exports = {
schema : false,
attributes: {
bookEnd : {
model : 'book'
},
user : {
model : 'user',
required : true
},
text : {
type : "text"
}
}
};

这是方法代码:

getAskedAdvices : function(req, res) { 

var queryAskedAdvices = AskedAdvices.find()
.populate("replayAdvices")
.populate("user")
.populate("bookStart")

queryAskedAdvices.exec(function callBack(err,askedAdvices){

if (!err) {
askedAdvices.forEach(function(askedAdvice, i){

askedAdvice.replayAdvices.forEach(function(reply, i){

async.parallel([
function(callback) {
var queryBook = Book.findOne(reply.bookEnd);
queryBook.exec(function callBack(err,bookEndFound) {
if (!err) {
reply.bookEnd = bookEndFound;
callback();
}
})
},
function(callback) {
var queryUser = User.findOne(reply.user)
queryUser.exec(function callBack(err,userFound){
if (!err) {
reply.user = userFound;
callback();
}

})
}
], function(err){
if (err) return next(err);

return res.json(200, reply);
})


})
})



} else {
return res.json(401, {err:err})
}
})
}

我可以使用异步库,但需要建议

谢谢大家!

最佳答案

正如评论中所指出的,Waterline 还没有深入的人口,但您可以使用 async.auto 来摆脱回调 hell 。诀窍是收集您需要查找的所有子项的 ID,通过单个查询找到它们,然后将它们映射回父项。代码如下所示。

async.auto({

// Get the askedAdvices
getAskedAdvices: function(cb) {
queryAskedAdvices.exec(cb);
},

// Get the IDs of all child records we need to query.
// Note the dependence on the `getAskedAdvices` task
getChildIds: ['getAskedAdvices', function(cb, results) {
// Set up an object to hold all the child IDs
var childIds = {bookEndIds: [], userIds: []};
// Loop through the retrieved askedAdvice objects
_.each(results.getAskedAdvices, function(askedAdvice) {
// Loop through the associated replayAdvice objects
_.each(askedAdvice.replayAdvices, function(replayAdvice) {
childIds.bookEndIds.push(replayAdvice.bookEnd);
childIds.userIds.push(replayAdvice.user);
});
});
// Get rid of duplicate IDs
childIds.bookEndIds = _.uniq(childIds.bookEndIds);
childIds.userIds = _.uniq(childIds.userIds);
// Return the list of IDs
return cb(null, childIds);
}],

// Get the associated book records. Note that this task
// relies on `getChildIds`, but will run in parallel with
// the `getUsers` task
getBookEnds: ['getChildIds', function(cb, results) {
Book.find({id: results.getChildIds.bookEndIds}).exec(cb);
}],

getUsers: ['getChildIds', function(cb, results) {
User.find({id: results.getChildIds.userIds}).exec(cb);
}]

}, function allTasksDone(err, results) {

if (err) {return res.serverError(err);

// Index the books and users by ID for easier lookups
var books = _.indexBy(results.getBookEnds, 'id');
var users = _.indexBy(results.getUsers, 'id');

// Add the book and user objects back into the `replayAdvices` objects
_.each(results.getAskedAdvices, function(askedAdvice) {
_.each(askedAdvice.replayAdvices, function(replayAdvice) {
replayAdvice.bookEnd = books[replayAdvice.bookEnd];
replayAdvice.user = users[replayAdvice.bookEnd];
});
});

});

请注意,这是假设 Sails 的内置 Lodash 和 Async 实例;如果您使用的是这些软件包的较新版本,async.auto 的用法已略有更改(任务函数参数已切换,以便 results 位于 cb 之前),并且 _.indexBy 已重命名为 _.keyBy

关于node.js - 无法找到一种简单的方法来摆脱每个 Node js 的多个异步(sails),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36999219/

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