diff --git a/handlers/dataHandlers.js b/handlers/dataHandlers.js index 09dd129..890a81f 100644 --- a/handlers/dataHandlers.js +++ b/handlers/dataHandlers.js @@ -353,19 +353,10 @@ Mark.pointList = function(req, res, next) { delete query.token; var points = []; - var cursor = mongoDB.find("camic", 'mark', query, { x: 1, y: 1, _id: 0 }); - - cursor.each((err, doc) => { - if (err) { - return next(err); - } - if (doc) { - points.push([doc.x, doc.y]); - } else { - req.data = { points: points }; - next(); - } - }); + mongoDB.forEach("camic", 'mark', query, false ,{ x: 1, y: 1, _id: 0 }).then((points) => { + req.data = { points: points.map(point => [point.x, point.y]) }; + next(); + }).catch((e) => next(e)); }; diff --git a/service/database/index.js b/service/database/index.js index e4bdefd..d628524 100644 --- a/service/database/index.js +++ b/service/database/index.js @@ -19,13 +19,13 @@ class Mongo { * * {@link https://docs.mongodb.com/manual/reference/method/db.collection.find/ Read MongoDB Reference} */ - static async find(database, collectionName, query, transform = true) { + static async find(database, collectionName, query, transform = true, filter = {}) { try { query = transformIdToObjectId(query); - + const collection = getConnection(database).collection(collectionName); - const data = await collection.find(query).toArray(); - + const data = await collection.find(query, filter).toArray(); + /** allow caller method to toggle response transformation */ if (transform) { data.forEach((x) => { @@ -34,13 +34,14 @@ class Mongo { }; }); } - + return data; } catch (e) { console.error(e); throw e; } } + /** * Runs the MongoDB find() method to fetch documents with pagination.