gpt4 book ai didi

javascript - collection.find mongojs同步回调

转载 作者:行者123 更新时间:2023-11-30 06:36:21 24 4
gpt4 key购买 nike

我正在尝试为 mongojs collection.find 方法编写自己的包装器,该方法应返回指定查询选择的集合项(查询尚未实现,它应该简单地选择所有结果)。问题是我没有返回一系列结果。看起来 find 方法做了某种异步回调。那么我该如何强制同步调用或强制我的脚本等待呢?

Collection.prototype.find = function () {
var result = new Array;
if (Bridge.isServer) {
db.collection(name).find(function(err, items) {
items.forEach(function(item) {
result.push(item);
});
});
}
return result;
}

最佳答案

我认为你应该考虑让你的函数异步,但如果你坚持写同步函数,有一个 github project用于使异步函数同步。

这是处理同一主题的另一篇 SO 帖子:Convert an asynchronous function to a synchronous function

关于javascript - collection.find mongojs同步回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14200417/

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