I'm attempting to utilize the .find
method in mongoose to retrieve all items in my collection. I then experimented with using a forEach
loop to create a variable that stores this information and pushes it into an array.
However, I've encountered a problem where the code doesn't seem to work as expected. Despite checking for errors through console logging, nothing was being returned. Am I implementing this function correctly?
[ { _id: 5b7e933aa5b5e7165fd02e77,
id: 7151,
location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
price: 5000,
door: 'X: -2.0055665969848633; Y: 20.169893264770508; Z: 71.10984802246094',
__v: 0 },
{ _id: 5b7e96042ba33b18dd03740f,
id: 6264,
location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
price: 5000,
door: 'X: -405.9184265136719; Y: 1160.5306396484375; Z: 325.9139404296875',
__v: 0 },
{ _id: 5b7e9664344f8c198f0fd266,
id: 1694,
location: 'X: 151.1869; Y: -1006.755; Z: -98.99999',
price: 5000,
door: 'X: -406.7716369628906; Y: 1162.4755859375; Z: 325.9158630371094',
__v: 0 } ]
House.find({}, function(err, house) {
console.log(house);
if(err) console.log(err);
house.forEach(function(h) {
let loadHouse = {
id: h.id,
location: JSON.parse(h.location),
price: h.price,
door: JSON.parse(h.door),
}
houses.push(loadHouse);
console.log(houses);
});