初始化后无法访问其他类中的实例

时间:2020-01-18 11:02:33

标签: python kivy

尝试将所有返回为None的变量从MyFirebase类访问Server类中的实例变量。我将firebase类初始化为Server类,我在App类中调用了MyFirebase.sign_up函数,该值仍返回为None。有人可以提供一些帮助吗?这是我的代码的相关部分。如果您需要我可以提供的MRE

MyFirebase类

class MyFireBase():

    def __init__(self):
        self.localId = None
        self.sign_up_request = None


    def sign_up(self, email, password):

            app = App.get_running_app()
            email = email.replace("\n","")
            password = password.replace("\n","")

            # Send email and password to Firebase
            # Firebase will return localId, authToken (idToken), refreshToken
            signup_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=" + self.wak
            signup_payload = {"email": email, "password": password, "returnSecureToken": True}
            self.sign_up_request = requests.post(signup_url, data=signup_payload)
            sign_up_data = json.loads(self.sign_up_request.content.decode())
            print(self.sign_up_request.ok)
            print(self.sign_up_request.content.decode())


            if self.sign_up_request.ok == True:
                print(sign_up_data)
                refresh_token = sign_up_data['refreshToken']

                self.localId = sign_up_data['localId']
                idToken = sign_up_data['idToken']

                # Save refreshToken to a file
                with open(app.refresh_token_file, "w") as f:
                    f.write(refresh_token)


                app.local_id = self.localId
                app.id_token = idToken




                my_data =  '{"avatar": "profilepic.png", "jobs_done": "", "jobs_posted": ""}'

                print(post_request.ok)
                print(post_request.content.decode())

                app.root.current = "create"



            elif self.sign_up_request.ok == False:

                error_data = json.loads(self.sign_up_request.content.decode())
                error_message = error_data["error"]['message']
                app.root.ids.signup.ids.signup_message.text = error_message.replace("_", " ")

    def send_user_details(self):
        app = App.get_running_app()
        my_data ={"first name": app.root.ids.create.ids.first_name.text, "last name": app.root.ids.create.ids.last_name.text,
                   "phone number": app.root.ids.create.ids.phone_number.text, "job1": app.root.ids.create.ids.job1.text, "job2": app.root.ids.create.ids.job2.text,
                   "job3": app.root.ids.create.ids.job3.text, "date of birth": app.root.ids.create.ids.date_of_birth.text, "state": app.root.ids.create.ids.state1.text}



    def sign_in_existing_user(self, email, password):
        signin_url = "https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=" + self.wak
        signin_payload = {"email": email, "password": password, "returnSecureToken": True}
        signin_request = requests.post(signin_url, data=signin_payload)
        sign_up_data = json.loads(signin_request.content.decode())
        app = App.get_running_app()
        print(signin_request.ok)
        print(signin_request.content.decode())

        if signin_request.ok == True:
            refresh_token = sign_up_data['refreshToken']

            self.localId = sign_up_data['localId']
            idToken = sign_up_data['idToken']
            # Save refreshToken to a file
            with open(app.refresh_token_file, "w") as f:
                f.write(refresh_token)


            app.root.current = "main"


        elif signin_request.ok == False:
            error_data = json.loads(signin_request.content.decode())
            error_message = error_data["error"]['message']
            app.root.ids.login.ids.login_message.text = error_message.replace("_", " ")


thefirebase = MyFireBase()

服务器类


class Server():

    def __init__(self, thefirebase, *args, **kwargs):
        super().__init__(*args, **kwargs)
        app = App.get_running_app()

        self.thefirebase = thefirebase  # Store the initialised MyFireBase instance


    def printlocalId(self):
        print(self.thefirebase.localId)

MyApp类以及如何调用sign_up函数


class MyApp(App):
    refresh_token_file = "refresh_token.txt"
    members_list = "members_list.txt"

    def build(self):
        self.thefirebase = MyFireBase()
        self.refresh_token_file = self.user_data_dir + self.refresh_token_file
        self.server = Server(thefirebase)

        return sm
        # delay your call to sign_up() until the App is built

        Clock.schedule_once(self.call_signup)



    def call_signup(self):
        thefirebasesignup = self.thefirebase.sign_up(email=self.root.ids.signup.ids.email.text, password=self.root.signup.ids.email.text)

任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

我认为您没有将刚刚构建的Firebase对象传递给服务器构造函数

在您的构建方法中:

def build(self):
        self.thefirebase = MyFireBase()
        self.refresh_token_file = self.user_data_dir + self.refresh_token_file
        self.server = Server(thefirebase)

        return sm
        # delay your call to sign_up() until the App is built

        Clock.schedule_once(self.call_signup)

您需要致电

self.server = Server(self.thefirebase)

此外,Clock.schedule_once(self.call_signup)不会执行,因为它是在return语句之后编写的。