Buckets
Buckets

Reputation: 89

Mongoose: Trying to do a forEach in the array of items in a collection

I'm trying to use the .find function in mongoose to get the entire amount of items in my collection. Then I tried using a forEach loop to set a variable using the information to push into an array.

For some reason it won't work, I tried console logging any errors but it wasn't returning anything, am I using 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);
});

Upvotes: 0

Views: 44

Answers (1)

Cohars
Cohars

Reputation: 4012

The errors seem to be within the forEach, you are trying to use JSON.parse() with non-valid json strings.

JSON.parse('X: -405.9184265136719; Y: 1160.5306396484375; Z: 325.9139404296875')

So for now you should remove the JSON.parse()

(Sorry I posted that in comment but should have done it as an answer)

Upvotes: 1

Related Questions