0

I have problems going further with my Promise that is returned from the getPostedPlaces(). When I run getAll() it prints out the Array seen below. The array seems correct, but how do I get the getAll() function to return an actual array. I can only print it, but I can't return the array itself. I have also tried to push the object inside the array onto an array, but the it returns empty. Frustrating.

const getPostedPlaces = async () => {
    const querySnapshot = await getDocs(collection(db, "posted_places"));
    const newSnapshot = querySnapshot.docs.map(doc => {
        let newDoc = doc.data();
        newDoc.id = doc.id;
        return newDoc;
    });
    return newSnapshot;
}


const getAll = () => {
    getPostedPlaces().then(res => console.log(res))
}

The array:

Array [
      Object {
        "active": true,
        "category": Array [
          "tøj",
          "møbler",
        ],
        "email": "bar@gmail.com",
        "house_nr": 1,
        "id": "i3juWf6Rj4OPBoKAjkBD",
        "location": Object {
          "latitude": 55.69718057,
          "longitude": 12.52470763,
        },
        "price_interval": Array [
          100,
          700,
        ],
        "street_name": "Hvidkildevej",
        "time_end_actual": "",
        "time_end_expected": "Sat Feb 05 2022 18:00:00 GMT+0100 (CET)",
        "time_start": "Sat Feb 05 2022 09:00:00 GMT+0100 (CET)",
        "zip_code": 2400,
      },
    ]
GoldenRetriever
  • 155
  • 3
  • 11
  • Do you mean you want to return the `res` parameter from your `.then` continuation from the `getAll` method? If so, you'll need to return a promise; you can't return the result of a promise before that promise has completed. – Richard Deeming Feb 09 '22 at 17:35
  • `getAll` does not return anything. – Heretic Monkey Feb 09 '22 at 17:35
  • I just want to have an array eventually. I don't care where it's returned. – GoldenRetriever Feb 09 '22 at 17:36
  • const getAll = async () => { const result = await getPostedPlaces() // This is your result } – Mohammed naji Feb 09 '22 at 17:37
  • It's more a question on how to turn a Promise into an array. – GoldenRetriever Feb 09 '22 at 17:37
  • Whatever you do, you can never return something *now* that will only be available in the *future*. That your promised value is an array is not the essential element of your question. The duplicate reference has all you need on the topic. – trincot Feb 09 '22 at 17:37
  • It will return if u write this way `const getAll = () => getPostedPlaces();`. But `getAll` will be a promise too so make sure u use `await` or `then` chain `getAll.then(...)`. – n1md7 Feb 09 '22 at 17:41

1 Answers1

1

You need to add a return since you are using {}

const getAll = async () => {
    // since you use {} you need to use return below
    return getPostedPlaces().then(res => {
      console.log(res)
      return res; //you need to return res here.
    })
}

Then when you use getAll, it has to be

await getAll()

Example where return is not needed, is when you don't use {}

const getAll = async() => getPostedPlaces()
Someone Special
  • 12,479
  • 7
  • 45
  • 76
  • true, i was writing that when I was writing a function with `await` inside. I've corrected it. Thanks for pointing it out. It's good habit to, however, write async when the function is involved. It makes function more obvious. – Someone Special Feb 09 '22 at 18:28