使用相同的方法

时间:2018-06-17 10:12:19

标签: firebase firebase-realtime-database vue.js firebase-authentication google-cloud-firestore

我正在教自己如何通过构建一个小项目站点进行编码,但是已经停留了几天,试图弄清楚如何更新用户的个人资料和帐户信息。我能够弄清楚如何检索和显示信息,但很难让他们更新它

目标:

  1. 更新他们在登录时使用的帐户电子邮件
  2. 更新存储在firestore中的全名(我想出了这个)
  3. 在Firestore中更新其用户的文档ID(用于其配置文件slug)
  4. 到目前为止,我已经能够弄清楚如何在表单字段中显示所有三个,但只有如何在Firestore中更新用户的全名,第1点和第3点仍在逃避我。

    对于目标2,我在此处https://firebase.google.com/docs/firestore/manage-data/add-data使用了Firestore文档中的“更新文档”,该文档成功运行

    为了更新用户电子邮件,我尝试使用此处https://firebase.google.com/docs/auth/web/manage-users

    中的更新电子邮件方法

    Screenshot of document in Firestore

    <template>
    
      <v-container fill-height>
        <v-layout justify-center align-center v-if="profile">
    
          <v-flex xs12 sm8 md8 style="max-width: 600px">
            <v-card >
              <v-toolbar dark color="primary">
                <v-toolbar-title>Profile</v-toolbar-title>               
              </v-toolbar>
    
    
              <v-flex class="ml-3 my-4">
                <v-avatar size="75px" class="mr-2" >
                            <img
                            class="img-circle elevation-2 "
                            src="https://raw.githubusercontent.com/vuetifyjs/docs/dev/static/doc-images/lists/1.jpg"
                            >
                        </v-avatar>
    
                        <v-btn color="primary" >Upload</v-btn>
                        <v-btn>Delete</v-btn> 
              </v-flex>
    
              <v-spacer></v-spacer>
              <v-divider></v-divider>
              <v-spacer></v-spacer>
              <v-card-text>
                <v-form>
                  <v-text-field 
                    prepend-icon="person" 
                    required 
                    v-model="profile.fullname" 
                    name="fullname" 
                    label="Full Name" 
                    type="text">
                  </v-text-field>
                  <v-text-field 
                    v-if="user" 
                    prepend-icon="email" 
                    required 
                    v-model="user.email" 
                    name="email" 
                    label="Email" 
                    type="text">
                  </v-text-field>
                  <v-text-field 
                    v-model="this.profile.id" 
                    hint="Create a unique URL for your profile. Many people use their first and last name. <br> [Ex: reel.ly/misha.collins]"
                    persistent-hint
                    id="username"  
                    prepend-icon="link" 
                    name="username" 
                    required
                    label="Profile URL " 
                    type="text">
                  </v-text-field>
                </v-form>
              </v-card-text>
              <v-card-actions>
                <v-spacer></v-spacer>
                <!-- <p class="red-text center" v-if="feedback">{{ feedback }}</p> -->
                <v-btn flat @click.native="updatemyProfile" color="primary">Save</v-btn>
              </v-card-actions>
            </v-card>
            <!-- <v-card style="margin-top: 30px" class="elevation-2">
              <v-toolbar dark color="primary">
                <v-toolbar-title>Billing</v-toolbar-title>               
              </v-toolbar>
              <v-card-text>
                <v-form>
    
                </v-form>
              </v-card-text>
              <v-card-actions>
                <v-spacer></v-spacer> 
              <p class="red-text center" v-if="feedback">{{ feedback }}</p> 
               <v-btn flat @click.native="updateBilling" color="primary">Update Account</v-btn>
              </v-card-actions>
            </v-card> -->
          </v-flex>
        </v-layout>
      </v-container>
    
    </template>
    
    <script>
    import db from '@/firebase/init'
    import firebase from 'firebase'
    import slugify from 'slugify'
    
    export default {
        name: 'Account',
        data () {
          return {
            user: null,
            profile: null,
            feedback: null,
            docid: null
          }
    
        },
    
        created () {
          let ref = db.collection('users')        
    
            // get current profile to list full name
            ref.where('user_id', '==', firebase.auth().currentUser.uid).get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    this.profile = doc.data(),
                    this.profile.id = doc.id
                })
            })
    
            // get current user to list email
            firebase.auth().onAuthStateChanged((user) => {
              if (user) {
                  this.user = user
    
                  // console.log(this.user)
              } else {
                  this.user.uid = null
              }
            })
    
    
        },
        methods: {
          updatemyProfile() {
            // working to change fullname but not document id
            let ref = db.collection('users')  
    
            // get current profile
            ref.where('user_id', '==', firebase.auth().currentUser.uid).get()
            .then(snapshot => {
                snapshot.forEach(doc => {
                    this.profile = doc.data(),
                    this.profile.id = doc.id
                })
            })
    
            var docRef = db.collection("users").doc(this.profile.id)
    
            return docRef.update({
              id: this.profile.id, // this is adding an id field and assigning this.profile.id value to it instead of updating the document id of the user
              fullname: this.profile.fullname
            })
    
            // update email address
            var user = firebase.auth().currentUser
              user.updateEmail(this.user.email).then(function() {
                console.log("success")
              }).catch(function(error) {
                // An error happened.
            })
            }
          }
    }
    
    </script>
    

    非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

查看函数末尾的这段代码:

false

您在调用$btn.on('click', function () { var $inputPageVal = $('input.page-number').val(); return showPage(parseInt($inputPageVal, 10)); }) 方法后,使用 var docRef = db.collection("users").doc(this.profile.id) return docRef.update({ id: this.profile.id, fullname: this.profile.fullname }) // update email address var user = firebase.auth().currentUser user.updateEmail(this.user.email).then(function() { console.log("success") }).catch(function(error) { // An error happened. }) 关键字从函数中提前返回,这意味着函数之后的任何代码(电子邮件地址的更新)都赢得了& #39; t跑步。