2

I have request to DB with promises, I want to extract 2 values("latitude" and "longitude") for all documents and put it to array.

I'm doing like this;

const promises = [
    dbo.collection("users").find({ name: "Mark" }, { latitude: 1, longitude: 1, _id: 0 }).toArray()
]
Promise.all(promises).then(function (Presults) {
        console.log(Presults);


    }).catch(function (err) {
        console.log(err);
    });

as a result I get all fields, for example: name, gender, latitude, longitude etc. When I try to specify that I want only latitude, longitude, I always get result undefined What I've tried: console.log(Presults.latitude); console.log(Presults[0].latitude);

But I want get output array like this

[
    { lat: -31.563910, lng: 147.154312 },
    { lat: -33.718234, lng: 150.363181 },
    { lat: -33.727111, lng: 150.371124 },
    { lat: -33.848588, lng: 151.209834 }
];

I tested without promises, it works:

    dbo.collection("users").find(
    { name: "Mark" },
    { latitude: 1, longitude: 1, _id: 0 }).toArray(function (err, result) {
        if (err) throw err;
        var array1 = [];
        var z = { "lat": result[0].latitude, "lng": result[0].longitude };
        var y = { "lat": result[0].latitude, "lng": result[0].longitude };
        array1.push(z, y);

        console.log(array1);
    });
5
  • can you show what exactly you get when you console.log(Presults); this Commented May 30, 2020 at 13:09
  • Could you add what exacly you got with console.log(Presults);? Commented May 30, 2020 at 13:10
  • try replacing {latitude: 1, longitude: 1, _id: 0} with '+latitude +longitude -_id'... Commented May 30, 2020 at 13:10
  • This is what I get: [ [ { _id: 'fiougisudfgy890-milmb:AP897rjhkgs9SooLQSqgXQqfPmHbIV166kL7ojR4n456734523AuOTv6KSbQZiLZZerty456jz7VwlqzQN', name: 'Mark', token: 'fPKrkfirog38960-milmb:APA91bGeK3OavT704978iofghbQZiLZZOHb6pLP39836ghqzQN', email: '[email protected]', Aid: '860a8ggirpww4565b2d', OS: '9', latitude: 26.521511, longitude: 52.1424 } ] ] Commented May 30, 2020 at 13:17
  • Ashwyn, this is not changing anything. It is just for search. My problem is with extraction data from result Commented May 30, 2020 at 13:20

1 Answer 1

1

Try this

Promise.all(promises).then(function (results) {
    const data = results.flatMap(v => v.map(k => ({ lat: k.latitude, lng: k.longitude})))

    console.log(data)
})
Sign up to request clarification or add additional context in comments.

7 Comments

it is not changing anything, it still returns full list of elements. I need to output only two parameters: latitude and longitude.
I have updated the answer, u need to remove toArray() from the promise first
output of your code is: [ { lat: undefined, lng: undefined } ] btw, valued in DB in double. I tried with toArray() and without - result is the same
can u show what comes when you do a console.log(results)
it is too big to post as a comment, it is smth like this [ Cursor { _readableState: ReadableState { objectMode: true, highWaterMark: 16,
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.