使用apisauce时,DRF APIView返回空数据对象,但在浏览器视图中不返回 [英] DRF APIView returns empty data object when using apisauce but not on browser view
问题描述
我正在尝试使用以下 APIview
函数查询模型以检查对象是否存在.不幸的是,我无法在react native apisauce get函数中收到任何响应对象,如下所示
I am trying to query a model to check if an object exists or not using the following APIview
function. Unfortunately i am not able to receive any response object in react native apisauce get function as shown below
apisauce功能
apisauce function
const checkIfUserExists = (email) =>
client.get("/account/user-exists/?email=" + email);
APIView类
class UserExistsView(APIView):
"""Checks if user exists or not"""
def get(self, request, *args, **kwargs):
username = self.request.GET['email']
try:
user = models.UserProfile.objects.get(email=username)
except models.UserProfile.DoesNotExist:
return Response(data={'message': False})
else:
return Response(data={'message': True})
浏览器的结果我在这里做错什么了?
Results from the browser What am i doing wrong here?
在外壳上打印结果<响应status_code = 200," text/html;charset = utf-8>
推荐答案
假设您有一个基于类的组件,则可以像下面那样修改函数,跟踪用户的状态.在您的服务器端代码中没有看到任何错误.但是在您的JavaScript代码中,您没有兑现承诺.
Assuming you have a class base component, You could modify your function like below, track the user existence in state. I don't see any errors in your server-side code. but in your javascript code you didn't handle the promise.
checkUserExists= (email) => {
client.get("/account/user-exists/?email=" + email)
.then(response=>{
this.setState({isUserExists : response.data.messgae})
)
}
这篇关于使用apisauce时,DRF APIView返回空数据对象,但在浏览器视图中不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!