1

I have a function defined in a file which calls an async function and also a normal function. I have defined them in this way

const cms_service = {
    fetchSinglePost: function(post){
        const image = await cms_service.fetchImageData(post)
        let single_post = {
            post: cms_service.fetchPostData(post),
            image: image
        }
        return single_post
    },
    fetchPostData: function (post) {
        var post_response = {
            id: post.data[0].id
        }
        return post_response
    },
    fetchImageData: async function (post) {
        let storyplay_response = {}
        if(typeof post.data[0].tmc_storyplay[0] !== 'undefined' && post.data[0].tmc_storyplay[0]){
            const storyplay = await axios.get(cms_service_url+"tmc_storyplay/"+post.data[0].tmc_storyplay[0], requestHeader);
            storyplay_response = {
                id: storyplay.data.id
            }
        }
        return storyplay_response
    }
}
module.exports = cms_service;

But it throws me an error saying

TypeError: this.fetchPostData is not a function

How do I correct this?

2 Answers 2

2

this keyword doesn't make sense in this context. Let's try to re-write by wrapping in object name as following:

const yourObj = {
 foo: () => 1,
 bar: () => yourObj.foo(),
}

module.exports = yourObject;
Sign up to request clarification or add additional context in comments.

12 Comments

I did the structure as per this. But I dont get the data returned from the async function "fetchImageData". How can I fix this?
@SaeeshTendulkar you have to use await with the asynchronous calls.
@VLAZ yeah. I tried it like this - post: await cms_service.fetchPostData(post). But it throws unknown identifier error
@SaeeshTendulkar I just noticed that fetchPostData doesn't return anything. It assigns a variable and that is it. Same thing with fetchImageData
Sorry I don't get what you're keen for. I saw a few problems in your code. If you wish to have something return of fetchImageData, you have to return your modified data like: return { id: storyplay.data.id }. Then you get the result: const yourData = await yourService.fetchImageData(yourPost)
|
0

You need to refactor your service a bit. You can access object key-values within that object.

const cms_service = () => {
    const fetchSinglePost = function(post){
        const image = await fetchImageData(post)
        let single_post = {
            post: fetchPostData(post),
            image: image
        }
        return single_post
    };

    const fetchPostData = function (post) {
        var post_response = {
            id: post.data[0].id
        }
        return post_response
    };

    const fetchImageData = async function (post) {
        let storyplay_response = {}
        if(typeof post.data[0].tmc_storyplay[0] !== 'undefined' && post.data[0].tmc_storyplay[0]){
            const storyplay = await axios.get(cms_service_url+"tmc_storyplay/"+post.data[0].tmc_storyplay[0], requestHeader);
            storyplay_response = {
                id: storyplay.data.id
            }
        }
        return storyplay_response
    };
}
module.exports = cms_service;

Comments

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.