0

I am calling an asynchronous function which fetches data and subsequently modifies it from another asynchronous function. The latter, however, does not wait until the data was modified in the function called. Currently it is only possible to return the data not anything handled inside the called function since the inside return is not waited for. I tried to async await for the called function but this did not work.

This is the initial call of the function

exports.addRequest = async (req, res) => {
    const requestResult = await RequestsModel.addRequestData(req.params.test)
    return res.send(requestResult);
};

And this is the function called

exports.addRequestData = async (test) => {

    await Requests.findById(requestId)
        .then((requestData) =>{
            //(01)
            if (requestData[type].filter(req => req[compareId] === requestDataObj[compareId]).length !== 0) return "exists";
            if (requestData[complementaryType].filter(req => req[complementaryCompareId] === requestDataObj[complementaryCompareId]).length !== 0) return "exists_complementary";

            //(02)
            requestData[type].push(requestDataObj);
            return requestData.save();
        }, (err) => {
            return err;
        });
};

I remove the parameters since they work fine. The only scenario where I return data is when I return the entire lower mongoose function body. For the promise and data handling the upper function does not seem to wait.

Any help is highly appreciated since I just started working with nodejs and asynchronous functions.

Thanks Jakob

6
  • 3
    You should restructure .addRequestData() so that it takes advantage of async and await. Currently, the function does not return anything. Mixing async/await with .then() is usually a sign that something is wrong. Commented Apr 12, 2021 at 12:40
  • So it makes more sense to return the fetched data from the .addRequestData() to the waiting function then handle it there and call another function which updates / removes etc. Commented Apr 12, 2021 at 12:42
  • 1
    Something like that. Basically, instead of .then(), assign the returned value of the await in that function to a variable. Then the code in the .then() moves directly into addRequestData(). Then the return statement returns back to the calling function. Now, I'm not sure what that .save() does; if it's also async you'd want to return the result of await on that call. Commented Apr 12, 2021 at 12:51
  • Ok I see. The logic behind this is that one user can request another and I double check if there is an existing request going on and if not I push the request as a sub document into a mongodb document and then save it afterwards.Thank your very much for your help! Commented Apr 12, 2021 at 12:54
  • requestData[type].push(requestDataObj); <-- where is requestDataObj defined? Commented Apr 12, 2021 at 12:55

1 Answer 1

1

Try modifying exports.addRequestData like this.

(I agree with Pointy)

exports.addRequestData = async (test) => {
    let requestData = await Requests.findById(requestId);
    if(requestData.isValid){ // success case
     // do operations on requestData and return it 
    } else { // error case
    // return error
    }
}

Also there is no mention of requestId. How is that?

Sign up to request clarification or add additional context in comments.

1 Comment

I removed all parameters. They worked fine. I implemented your approach and it worked just fine. Thanks

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.