link822 link823 link824 link825 link826 link827 link828 link829 link830 link831 link832 link833 link834 link835 link836 link837 link838 link839 link840 link841 link842 link843 link844 link845 link846 link847 link848 link849 link850 link851 link852 link853 link854 link855 link856 link857 link858 link859 link860 link861 link862 link863 link864 link865 link866 link867 link868 link869 link870 link871 link872 link873 link874 link875 link876 link877 link878 link879 link880 link881 link882 link883 link884 link885 link886 link887 link888 link889 link890 link891 link892 link893 link894 link895 link896 link897 link898 link899 link900 link901 link902 link903 link904 link905 link906 link907 link908 link909 link910 link911 link912 link913 link914 link915 link916 link917 link918 link919 link920 link921 link922 link923 link924 link925 link926 link927 link928 link929 link930 link931 link932 link933 link934 link935 link936 link937 link938 link939 link940 link941 link942 link943 link944 link945 link946 link947 link948 link949 link950 link951 link952 link953 link954 link955 link956 link957 link958

[Vue.js] Api response is null when consumed, with django and vue

Im using my api from the rest framework, when I got to the URL from the api it shows’s it correct. But when I consume it the response seems to be null.
Im hosting both project (front vuejs and back django) in diferents ports (8080 and 8000, respectively). CORS are allowed.

I’ve tried with another tools like fetch or jquery and seems to be the same.


export class APIService {
contructor() {}

const url = `http://localhost:8000/api/v1/user-id/\`;
return axios.get(url).then(function (response) {
console.log(response); // For testing

Django api view, for that url:

class UserIdLoggedView(APIView):
def get(self, request):
user = request.user
return Response({

For this case I didn’t need a serializer.

This is how the django rest shows it:

{ HTTP 200 OK
Content-Type: application/json
Vary: Accept

“id”: 1

And this is the output of the response from the console (just the data part)

id: null

Instead of return id=1 as expected it return null.

Solution :

why are you using two return statements in get_user_id? You hit the first return statement and it doesn’t wait for the promise to be fulfilled. Basically, you send the get request but don’t wait for the response. Also, I would recommend creating a base instance of axios as a vue.js plugin.