I’m having trouble making a function: promise

Hi,
I’m new to MongoDB and javascript. I’ve been making progress with the tutorials and documentation.
I am currently working on a geospatial data exercise.

The data is loading to MongoDB correctly. The geospatial index is working, I have verified it by running a find command in compass.

The following works in mongosh:

db.icao.find(
   {
     location:
       { $near:
          {
            $geometry: { type: "Point",  coordinates: [-6.3820318, 53.4226481 ] },
            $minDistance: 1000,
            $maxDistance: 55000
          }
       }
   }
)

This produces valid output in the function console for testing:

   return collection.find({ 'location':
 	  { $near :
 	    { $geometry:
 	      { type: "Point",  coordinates: [ -6.3820318, 53.4226481 ] },
 	        $maxDistance: 550000
 	    }
 	  }

but I can’t make it into a promise. This is what I have

exports = async function (request, response) {
    let collection = context.services.get("mongodb-atlas").db("ICAO").collection("icao");

    collection.find({ 'location':
            { $near :
                    { $geometry:
                            { type: "Point",  coordinates: [ -6.3820318, 53.4226481 ] },
                        $maxDistance: 550000
                    }
            }
    })
        .then(result => {
            if (result != null){
                // response.setBody(JSON.stringify({ "messageCode": 200, "message": "ok" }));
                // const resultJson = JSON.stringify(result);
                console.log(`bob  `);

                response.setBody(JSON.stringify(result));
                // response.setBody(JSON.stringify(result));
                response.setStatusCode(200); // Set an HTTP Status code like "200 - OK"
            } else {
                response.setStatusCode(404); // Set an HTTP Status code not found"
                response.setBody(JSON.stringify({ "code": 404, "message": "not found" }));
            }
        })
        .catch(err => console.error(`Fatal error occurred: ${err}`));
}

Postman error

{

    "error": "{\"message\":\"'then' is not a function\",\"name\":\"TypeError\"}",

    "error_code": "FunctionExecutionError",

    "link": "https://realm.mongodb.com/groups/62176c675ae126260fadac3a/apps/62176d262e0e07eaf541e9c2/logs?co_id=6223745213345990845dbe7a"

}

sorted. 5 minutes after I posted I found the answer.

https://mongodb.github.io/node-mongodb-native/2.2/tutorials/geospatial-search/

collection.find(
	{ 'address.coord':
	  { $near :
	    { $geometry:
	      { type: "Point",  coordinates: [ -73.9667, 40.78 ] },
	        $maxDistance: 1000
	    }
	  }
	}
  ).toArray(function(err, docs) {
    assert.equal(err, null);
    console.log("Found the following records");
    console.log(docs);
    callback(docs);
  });

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.