0

I am trying to save bunch of tags into the db and want to get the id in return but I keep on getting the [ Promise { <pending> } ]

    const tagsArray = tags.split(',');
    const tagIds = await tagsArray.map(async name => {
        return await Tags.findOneAndUpdate({
            name
        }, {
            $addToSet: { user }
        }, {
            new: true,
            upsert: true
        }).then(result => {
            return result;
        })
    });

when I try to console.log(tagIds) I only get [ Promise { <pending> } ]

I used to write without the .then() but still doesn't work though.

How can I get the result?

Thanks in advance for any help.

Tsuna
  • 2,098
  • 6
  • 24
  • 46
  • You're `.map`ping each tag to a `Promise` in an array, but you can't `await` arrays (meaningfully), you can only `await` Promises – CertainPerformance Sep 19 '18 at 00:07
  • @CertainPerformance so I shouldn't be using `Promise` ? – Tsuna Sep 19 '18 at 00:09
  • Didn't say that - check out the linked question – CertainPerformance Sep 19 '18 at 00:10
  • @CertainPerformance sryz I meant `map` not `Promise`. I will read the link :D thx thx – Tsuna Sep 19 '18 at 00:13
  • @CertainPerformance thanks for the link, I think I am posting the question wrong. I should be asking how to `push` into an array in a promise. Let me re-edit my question – Tsuna Sep 19 '18 at 00:23
  • 1
    You can only await `Promises`, so, change to `const tagIds = await Promise.all(tagsArray.map...` – CertainPerformance Sep 19 '18 at 00:25
  • @CertainPerformance thx thx a lot, I was using `Promise.all(tagIds).then().....` which didn't work too so was trying it. But putting `Promise.all()` right in front works like a charm. Thanks thanks – Tsuna Sep 19 '18 at 00:40

0 Answers0