# How can I make this async/.then code work?

**URL:** <https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [March 15, 2020, 7:27pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763 "2020-03-15T19:27:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sekijohn10](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sekijohn10/32/17696_2.png) [@sekijohn10](https://forum.wixstudio.com/u/sekijohn10)\
**Post date:** [March 15, 2020, 7:27pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/1 "2020-03-15T19:27:36Z")

</div>

I am having issues fully grasping the Promise and Async aspects of the backend in wix. I have the following code that isn’t in the proper sync:

Code on the frontend:

```javascript
confirmPokemonId($w('#input1'))
.then( (results) => {
wixLocation.to("/searched-pokemon-1/" + results.items[0].title);
});

```

Code on the backend:

```javascript
export async function confirmPokemonId(input) {
    let temp = await wixData.query("SearchedPokemon").eq("pokemonId", input.value).find();
    if(temp.items.length > 0) {
        return temp;
    } else {
        let obj = await apiCall("pokemon", input.value);
        return await addPokemon(obj);
    }
}

export function addPokemon(obj) {
...code removed for brevity...
    
    return wixData.query("SearchedPokemon")
    .eq("title", item.title)
    .find(options)
    .then( (results) => {
        if(results.items.length > 0) {
            return results;
        } else {
            return wixData.insert("SearchedPokemon", item, options)
            .then( temp => temp);
        }
    });
}

```

What am I doing wrong? I have two theories. Either I don’t understand what WixData.insert returns. Or WixLocation is executing before the Pokemon is added to the Database.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [March 15, 2020, 8:40pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/2 "2020-03-15T20:40:13Z")

</div>

The result of wixData.insert() is not an array but a single object.  
So you cannot do: results.items[0].title.  
Instead you should use: results.title.

---

<div class="post-metadata">

**Author:** ![J.D](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/j.d/32/7312_2.png) [@J.D](https://forum.wixstudio.com/u/J.D)\
**Post date:** [March 15, 2020, 8:44pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/3 "2020-03-15T20:44:44Z")

</div>

Also it’s not clear what you’re trying to do there.  
You first retrieve the item then you don’t change it but re-inset it? What’s the point?

- I don’t see where you define the variable “options” (maybe it’s in the code you removed)

---

<div class="post-metadata">

**Author:** ![sekijohn10](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sekijohn10/32/17696_2.png) [@sekijohn10](https://forum.wixstudio.com/u/sekijohn10)\
**Post date:** [March 15, 2020, 10:23pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/4 "2020-03-15T22:23:40Z")

</div>

No I’m making sure the item isn’t in the dataset. If it is then I don’t add another copy. If it isn’t then I add it to the dataset. Options is in the removed code, it isn’t related to issue.

---

<div class="post-metadata">

**Author:** ![sekijohn10](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sekijohn10/32/17696_2.png) [@sekijohn10](https://forum.wixstudio.com/u/sekijohn10)\
**Post date:** [March 15, 2020, 10:24pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/5 "2020-03-15T22:24:18Z")

</div>

Thank you. That sounds like the problem. I will try that and let you know if it works.

---

<div class="post-metadata">

**Author:** ![sekijohn10](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/sekijohn10/32/17696_2.png) [@sekijohn10](https://forum.wixstudio.com/u/sekijohn10)\
**Post date:** [March 15, 2020, 10:28pm UTC](https://forum.wixstudio.com/t/how-can-i-make-this-async-then-code-work/31763/6 "2020-03-15T22:28:41Z")

</div>

This was my problem. Thank you very much!
