Home>

Django creates a web application and uses Vue.js in Template.
I'm accessing the database using AXIOS, and the data passing itself is good, but I'm going to access this .Then () from .This. $ DATA to be undified if I try to access this .Then ().

MyScript.js

NEW VUE ({
    el: '#article',
    Delimiters: [[[',']] '],
    Data: {
        Gooded_ArticleS: [],
    }
    CREATED: FUNCTION () {
        //(1)
        console.log (this.gooded_articles);
        axios.get ('/snsapp /gooded_articles /')
            .Then (Function (Response) {
                //(2)
                console.log (this.gooded_articles);
                var article_ids= response.data.article_ids;
                //(3)
                console.log (ARTICLE_IDS);
                for (Var i= 0; i <
 Article_ids.length; i ++) {
                    gooded_articles.push (ARTICLE_IDS [i]);
                }
            })
            .catch (Function (Error) {
                Console.log (Error);
            })
    }
}))

URLS.PY

URLPATTERNS= [
    path ('gooded_articles /', views.goodedarticles.as_view (), name= 'gooded_articles'),
]

views.py

Class Goodedarticles (APIVIEW):
    Authentication_classes= (Authentication.SessionAuthentication,)
    permission_classes= (Permissions.isauthenticated,)
    DEF Get (Self, Request):
        good_instances= good.Objects.Filter (PUSHER= REQUEST.USER)
        Article_ids= []
        for Good_Instance in Good_Instances:
            gooded_article= good_instance.article
            Article_ids.append (gooded_article.id)
        DATA= {
            'article_ids': ARTICLE_IDS,
        }
        Return JSONResponse (Data, Safe= False)

Console Results
-(1):[__OB__: OBSERVER] LENGTH: 0
-(2):undefined
-(3):(2) [1, 3]
-ReferenceError: gooded_articles is not defined

In the code that has been referred to .then (), it has been accessed by this. $ DATA, so I do not know the cause.
Thank you.

  • Answer # 1
    Since this is an AXIOS object in

    .then (), it was solved by binding with .bind (this) or writing in the arrow function so that this does not change.