简体   繁体   中英

collection.find mongojs synchronous callback

I'm trying to write my own wrapper for the mongojs collection.find method that should return the collection items selected by the specified query (querying isn't implemented yet, it should simply select all results). The problem is that I'm not getting back an array of results. It seems like the find method does some kind of async callback. So how can I force a synchronous call or force my script to wait?

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;
}

I think you should consider making your functions asynchronous, but if you insist on writing synchronous functions, there is a github project for making async functions synchronous.

Here's another SO post dealing with the same topic: Convert an asynchronous function to a synchronous function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM