0

我正在用 Flask & Pyrebase 创建一个简单的目录;我已经成功地创建了登录名和注册表单。登录和注册后,用户将被重定向到他们的个人资料页面。

我遇到的问题是当我尝试添加新条目时,我的现有数据被覆盖。当我创建一个新的数据库条目时,我使用 set() 而不是 push() 我想这可能是为什么?

我已经尽可能地缩短了我们的代码

创建帐户和注册业务:

@app.route('/register', methods=['GET', 'POST'])
def register():
unsuccessful = 'Please check your credentials'
successful = 'Registraion successful'
    if request.method == 'POST':
        email = request.form.get('email')
        confirmEmail = request.form('confirmEmail')
        password = request.form.get('pass')
        userName = request.form.get('inputName')
        businessName = request.form.get('businessName')
        startYear = request.form.get('startYear')
        selectCategory = request.form.get('selectCategory')
        businessDescription = request.form.get('businessDescription')
        businessAddress = request.form.get('businessAddress')
        businessTown = request.form.get('businessTown')
        businessCity = request.form.get('businessCity')
        businessPostcode = request.form.get('businessPostcode')
        businessNumber = request.form.get('businessNumber')
        businessEmail = request.form.get('businessEmail')
        bameRegister = dict(
            userName = userName,
            confirmationEmail = confirmEmail,
            businessName = businessName,
            businessStartYear = startYear,
            businessCategory = selectCategory,
            businessDescription = businessDescription,
            businessAddress = [businessAddress, businessTown, businessCity, businessPostcode],
            businessNumber = businessNumber,
            businessEmail = businessEmail,
        )
        if selectCategory == "arts":
            try:
                user = auth.create_user_with_email_and_password(email, password)
                auth.send_email_verification(user['idToken'])
                db.child("Bame_Buisness").child("business").child("arts").set(bameRegister, user['idToken'])
                return render_template('homepage.html', x=successful)
            except:
                return render_template('homepage.html', y=unsuccessful)
        elif selectCategory == "food":
            try:
                user = auth.create_user_with_email_and_password(email, password)
                auth.send_email_verification(user['idToken'])
                db.child("Bame_Business").child("business").child("food").set(bameRegister, user['idToken'])
                return render_template('homepage.html', x=successful)
            except:
                return render_template('homepage.html', y=unsuccessful)
       elif selectCategory == "health":
           try:
               user = auth.create_user_with_email_and_password(email, password)
               auth.send_email_verification(user['idToken'])
               db.child("Bame_Business").child("business").child("health").set(bameRegister, user['idToken'])
               return render_template('homepage.html', x=successful)
            except:
               return render_template('homepage.html', y=unsuccessful)
    return render_template('homepage.html')

创建登录:

@app.route('/login', methods=['GET', 'POST'])
def login():
    unsuccessful = 'Please check your credentials'
    if request.method == 'POST':
        try:
            user = auth.sign_in_with_email_and_password(email, password)
            signed_in_user = auth.get_account_info(user['idToken'])
            artCategory = db.child("Bame_Business").child("business").child("arts").get(user['idToken']).val()
            foodCategory = db.child("Bame_Business").child("business").child("food").get(user['idToken']).val()
            healthCategory = db.child("Bame_Business").child("business").child("health").get(user['idToken']).val()
            if signed_in_user['users'][0]['email'] == artCategory['confirmationEmail']:
                return render_template('profile.html', artCategory=artCategory)
            elif signed_in_user['users'][0]['email'] == foodCategory['confirmationEmail']:
                return render_template('profile.html', foodCategory=foodCategory)
            elif signed_in_user['users'][0]['email'] == healthCategory['confirmationEmail']:
                return render_template('profile.html', healthCategory=heathCategory)
        except:
            return render_template('homepage.html')
    return render_template('homepage.html')

这是登录后创建新业务的途径:

@app.route('newBusiness', methods=['GET', 'POST'])
def newBusiness():
    if request.method == 'POST':
        email = request.form.get('email')
        confirmEmail = request.form.get('confirmEmail')
        password = request.form.get('pass')
        userName = request.form.get('userName')
        businessName = request.form.get('businessName')
        startYear = request.form.get('startYear')
        selectCategory = request.form.get('selectCategory')
        businessDescription = request.form.get('businessDescription')
        businessAddress = request.form.get('businessAddress')
        businessTown = request.form.get('businessTown')
        businessCity = request.form.get('businessCity')
        businessPostcode = request.get('businessPostcode')
        businessNumber = request.form.get('businessNumber')
        businessEmail = request.form.get('businessEmail')
        bameRegister = dict(
            userName = userName,
            confirmationEmail = confirmEmail,
            businessName = businessName,
            businessStartYear = startYear,
            businessCategory = businessCategory,
            businessDescription = businessDescription,
            businessAddress = [businessAddress, businessTown, businessCity, businessPostcode],
            businessNumber = businessNumber,
            businessEmail = businessEmail,
        )
        if selectCategory == "arts":
            try:
                user = auth.sign_in_with_email_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("arts").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                artCategory = db.child("Bame_Business").child("business").child("arts").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == artCategory['confirmationEmail']:
                   return render_template('profile.html', artCategory=artCategory)
            except:
                return render_template('homepage.html')
        elif selectCategory == "food":
            try:
                user = auth.sign_in_with_email_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("food").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                foodCategory = db.child("Bame_Business").child("business").child("food").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == foodCategory['confirmationEmail']:
                    return render_template('profile.html', foodCategory=foodCategory)
            except:
                return render_template('homepage.html')
        elif selectCategory == "health":
            try:
                user = auth.sign_in_with_emal_and_password(email, password)
                user = auth.refresh(user['refreshToken'])
                db.child("Bame_Business").child("business").child("health").set(bameRegister, user['idToken'])
                signed_in_user = auth.get_account_info(user['idToken'])
                healthCategory = db.child("Bame_Business").child("business").child("health").get(user['idToken']).val()
                if signed_in_user['users'][0]['email'] == healthCategory['confirmationEmail']:
                    return render_template('profile.html', healthCategory=healthCategory)
            except:
                return render_template('homepage.html')
    return render_template('homepage.html')

用户登录后,假设他们在艺术类别中注册了他们的第一个业务,当他们在艺术类别中注册他们的第二个不同业务时,原始收藏在 Firebase 中被覆盖。即使选择不同的类别也会发生这种情况。这绝对不理想,因为有人可能会覆盖其他人的数据——这会导致各种问题!

我了解您可以使用 push & set 在 pyrebase 中创建新条目;我选择 set 是因为当我使用 push 时,它会创建一个独特的标签,我发现很难从用户帐户中获得个人访问权限。使用 set 方法,可以很容易地用artCategory['businessName']orartCategory['businessDescription']artCategory['confirmationEmail']etc调出我想要的字典部分

如果有人可以帮助我提出一个非常感谢的解决方案,我只需要在登录后创建一个新的商业注册而不覆盖任何现有数据。

亲切的问候

4

1 回答 1

1

.set()覆盖。 .update()仅更新已更改的字段。

于 2020-07-31T00:08:12.260 回答