Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

firebase - Firebase设置方法未将数据添加到现有集合(Firebase set method not adding data to an existing Collection)

I am using VUEX and Firebase to create a register form with three fields NAME, EMAIL, PASSWORD.

(我正在使用VUEX和Firebase创建具有三个字段NAME,EMAIL,PASSWORD的注册表。)

First i am using createUserWithEmailAndPassword method to add the user but I also want to ad the name filed data to an Existing Blank collection, here I am using set method.

(首先,我正在使用createUserWithEmailAndPassword方法添加用户,但我也想将名称字段数据添加到“现有空白”集合中,这里我使用set方法。)

But it is not adding the name field data in the collection.

(但这不是在集合中添加名称字段数据。)

methods: {
onSignUp() {
        firebase
        .auth()
        .createUserWithEmailAndPassword(this.email, this.password)
        .then(user => {
            console.log(user);
            console.log(user.user.uid);

            firebase.database.collection("profiles").doc(user.user.id).set({
                name: this.name
            })
            .then(function() {
                console.log("Document successfully written!");
            })
            .catch(function(error) {
                console.error("Error writing document: ", error);
            });

            this.$store.dispatch('signUserUp', user);

        })
        .catch(error => {
            this.$store.dispatch('signUserError', error)
        });
    }
}

data(){
  return {
    name: "",
    email: "",
    password: "",
  }
}

After submitting the form it's adding a new user and I can also see the uid in the console but some how its not updating the name field in the database.

(提交表单后,它添加了一个新用户,我还可以在控制台中看到uid,但是有些不更新数据库中的name字段。)

  ask by RAHUL KUNDU translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use firebase.firestore() and not firebase.database (See this doc ) and therefore adapt your code as follows:

(您应该使用firebase.firestore()而不是firebase.database (请参阅此文档 ),因此请按以下方式修改代码:)

onSignUp() {
        firebase
        .auth()
        .createUserWithEmailAndPassword(this.email, this.password)
        .then(user => {
            console.log(user);
            console.log(user.user.uid);

            return firebase.firestore().collection("profiles").doc(user.user.id).set({
                name: this.name
            });
         })
         .then(() => {
             console.log("Document successfully written!");
             this.$store.dispatch('signUserUp', user);
         })
        .catch(error => {
            this.$store.dispatch('signUserError', error)
        });
    }
}

You should also check that your security rules for the profiles collection are correctly set.

(您还应该检查是否正确设置了profiles收集的安全规则。)

Normally (authenticated) users should only be able to write a document with their own userid as document id.

(通常,(经过身份验证的)用户只能使用自己的userid作为文档ID编写文档。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...