我有两个集合Stud
和Prof
。
我有一个函数,它以id
为参数,如果id属于任何一个集合,则返回相应的信息。
id
集合的Prof
id
集合的Stud
预期结果:首先获得Prof
结果,其次是Stud
结果。
但是由于nodejs的异步特性,我总是先得到Stud
结果,然后才得到Prof
结果。
是否通过引入新变量或更改查询集合的方式来完成此任务?
如有任何帮助,将不胜感激。
var check_user_info = function(userid, callback) {
Stud.findOne({
'_id': userid
}, function(err, stud) {
if (err)
throw err
if (stud) {
callback(stud);
} else {
Prof.findOne({
'_id': userid
}, function(err, prof) {
if (err)
throw err
if (prof) {
callback(prof);
} else {
callback(false);
}
})
}
})
return
}
发布于 2017-09-15 01:20:51
作为前面的答案,您可以使用异步模块来完成此任务。控制节点的非阻塞性有很多功能.在这里,我将建议您使用“并行”方法。由于查询是相互独立的,它将比“瀑布”方法更快。
根据您的问题,代码将如下所示。
var async = require('async');
async.parallel(
[
(cb) => {
Stud.findOne(
{
_id: userid
},
cb
);
},
(cb) => {
Prof.findOne(
{
_id: userid
},
cb
);
}
],
(err, result) => {
if (err) {
//handle error
return;
}
//result will be an array where the first element will be the result of first query and
// second element will be the query result for the second query
// so according to this .....
if (result[0]) {
//id is matched with Stud collection
//result[0] is the student doc
} else if (result[1]) {
//id is matched with Prof collection
//result[0] is the professor doc
} else {
//Neither Stud or Prof
}
}
);
您可以从asyn文档中了解有关异步方法的信息。
发布于 2017-09-14 23:26:39
发布于 2017-09-14 23:43:24
要回答你的问题,请申请@abdulbarik贴子。
下面是关于您的实际代码的其他内容:
_id
密钥放入引号中备注:
有关回调和功能削减的示例。我让你做剩下的事,就是es6,瀑布处理.您可以查看诺言和异步/等待模式。
// Check if there is a student
function check_student(user_id, callback) {
Stud.findOne({
_id: user_id
}, function (err, stud) {
if (err) return callback(err, false);
// stud here can worth false
return callback(false, stud);
});
}
// Check if there is a prof
function check_prof(user_id, callback) {
Prof.findOne({
_id: user_id
}, function (err, prof) {
if (err) return callback(err, false);
// prof here can worth false
return callback(false, prof);
});
}
// Get Stud not Prof info
function check_user_info(user_id, callback) {
// Look if user_id match a stud
check_student(user_id, function (err, result) {
// We have an error
if (err) return callback(err, false);
// We have a student
if (result) return callback(false, result);
// Check if user_id match a prof
check_prof(user_id, function (err, result) {
// We have an error
if (err) return callback(err, false);
// We have a prof
if (result) return callback(false, result);
// No result at all
return callback(false, false);
});
});
}
你怎么称呼它
check_user_info(user_id, function (err, result) {
// ...
});
具有承诺的代码示例:
// Check if there is a student
function check_student(user_id) {
return new Promise((resolve, reject) => {
Stud.findOne({
_id: user_id
}, (err, stud) => {
if (err) return reject(err);
// prof here can worth false
return resolve(stud);
});
});
}
// Check if there is a prof
function check_prof(user_id) {
return new Promise((resolve, reject) => {
Prof.findOne({
_id: user_id
}, (err, prof) => {
if (err) return reject(err);
// prof here can worth false
return resolve(prof);
});
});
}
// Get Stud not Prof info
function check_user_info(user_id) {
return Promise.all([
check_student(user_id),
check_prof(user_id),
]);
}
check_user_info(user_id)
.then([
stud,
prof,
] => {
// Handle result
})
.catch((err) => {
// Handle error
});
https://stackoverflow.com/questions/46233922
复制