gpt4 book ai didi

node.js - Mongoose : find data by looping on an array of models

转载 作者:太空宇宙 更新时间:2023-11-03 23:08:56 25 4
gpt4 key购买 nike

我陷入了异步算法的困境:

我有一系列 Mongoose 模型:

var allRefDatasSchemas = {
RefAllotement: mongoose.model('RefAllotement', RefDataSchema),
RefModeleConstructeur: mongoose.model('RefModeleConstructeur', RefDataSchema),
RefTypeKit: mongoose.model('RefTypeKit', RefDataSchema),
RefTypeUtilisation: mongoose.model('RefTypeUtilisation', RefDataSchema),
};

我想获取每个集合的所有项目并将它们放入数组或类似的东西中。如果我这样做,find 回调的 this 关键字不会引用当前模型,我不可能知道哪些模型项目属于

var results = {};

for (var model in allRefDatasSchemas) {

allRefDatasSchemas[model].find(function(err, data) {

// I'd like to do something like that :
// but this.modelName is null, because it isn't the model
// on which the find is done.
results[this.modelName] = data;

// if I use "model" variable, it doesn't work, because asynchronous callback

});

}

我也尝试过async库没有成功,因为我总是返回相同的问题:不可能知道哪个模型在回调中执行查找查询。如果我使用 Promise,则同上 then

请帮助我:)你会怎么做?

编辑model.find 调用 query.find,query.find 调用 mquery.find。在 mquery.find 中,通过丢失 this 引用来调用回调: this._collection.find(conds, options, utils.tick(callback));/编辑

最佳答案

请检查此代码片段,我已经制作了您需要的工作示例。请检查代码中的注释以便更好地理解。

Sample Working code与您所要求的类似。另ref ques用于将异步与 Mongoose 一起使用。

/*
* Object to store all models
*/
var allRefDatasSchemas = {
RefAllotement: mongoose.model('RefAllotement', RefDataSchema),
RefModeleConstructeur: mongoose.model('RefModeleConstructeur', RefDataSchema),
RefTypeKit: mongoose.model('RefTypeKit', RefDataSchema),
RefTypeUtilisation: mongoose.model('RefTypeUtilisation', RefDataSchema),
};
/*
* need an array to run all queries one by one in a definite order using async waterfall mwthod
*/
var arr = [];
for(each in allRefDatasSchemas) {
arr.push(each);
}

/*
* Callback function for initiation of waterfall
*/
var queue = [
function(callback) {
// pass the ref array and run first query by passing starting index - 0
callback(null, arr, 0)
}
];

/*
* Object to store result of all queries
*/
var finalResult = {};

/*
* Generic Callback function for every dynamic query
*/
var callbackFunc = function(prevModelData, currentIndex, callback) {
allRefDatasSchemas[arr[currentIndex]].find(function(err, result) {
if(err) {
console.log(err)
} else {

// Your Query
//
// I'd like to do something like that :
// but this.modelName is null, because it isn't the model
// on which the find is done.

// arr[currentIndex] will point to
// RefAllotement, RefModeleConstructeur etc. as you required
finalResult[arr[currentIndex]] = result

// send current result to next interation if required or you can skip
// and increment the currentIndex to call next query
callback(null, result, currentIndex + 1)
}
})
}

/*
* Add callback function for every dynamic query
*/
for(each in allRefDatasSchemas) {
queue.push(callbackFunc);
}

/*
* Run all dynamic queries one by one using async.js waterfall method
*/
async.waterfall(queue, function (err, result) {
// Final object with result of all the queries
console.log('finish', finalResult)
});

输出将采用此格式

finish { RefAllotement:[
// Result of RefAllotement query
],
RefModeleConstructeur:[
// Result of RefModeleConstructeur query
],
RefTypeKit:[
// Result of RefTypeKit query
],
RefTypeUtilisation:[
// Result of RefTypeUtilisation query
]
}

关于node.js - Mongoose : find data by looping on an array of models,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24185367/

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