简体   繁体   English

为什么我的功能无法识别其他功能?

[英]Why does my function not recognise my other function in react?

Why do I get the error: Uncaught ReferenceError: writeUserData is not defined(…) 为什么会出现错误: Uncaught ReferenceError: writeUserData is not defined(…)

or the other error when i include this.writeUserData which is: Cannot read property 'writeUserData' of undefined 或当我包含this.writeUserData时出现其他错误: Cannot read property 'writeUserData' of undefined

The function still posts the correct data to firebase so why is it complaining? 该函数仍将正确的数据发布到Firebase,为什么会抱怨呢? and more so what is the correct way to call a function inside another function. 等等,在另一个函数中调用一个函数的正确方法是什么? i thought this was the right way to do it? 我认为这是正确的方法?

signUserUp(){
    let name = this.state.nameValue;
    let slackName = this.state.slackNameValue;
    let email = this.state.emailValue;
    let password = this.state.passwordValue;
    firebase.auth().createUserWithEmailAndPassword(email, password).then(
      function(result){
        writeUserData(name, result.uid, email, slackName);
      },
      function(error){
        let errorCode = error.code;
        let errorMessage = error.message;
        console.log(errorMessage, '***');
        console.log(errorCode, 'code');
        });
    };

    writeUserData(name, useruid, email, slackName){
      firebase.database().ref('/pairs/' + useruid).set({
        name: name,
        useruid: useruid,
        email: email,
        slackName: slackName
      });
    }

EDIT: This code is inside a React component using ES6 syntax... 编辑:此代码在使用ES6语法的React组件中...

Your just need to declare it as a function: 您只需要将其声明为一个函数:

function writeUserData(name, useruid, email, slackName){...};

You think that it succeeds only because createUserWithEmailAndPassword() creates your user, and only after that calls the success handler. 您认为它成功仅是因为createUserWithEmailAndPassword()创建您的用户,并且仅在此之后调用成功处理程序。

But this is actually never executed in your case: 但这实际上不会在您的情况下执行:

firebase.database().ref('/pairs/' + useruid).set

For you first question: The reason is that you didn't declare writeUserData as a function. 对于您的第一个问题:原因是您没有将writeUserData声明为一个函数。

Here is a way similar to Alexey Soshin's answer, but with an alternative syntax. 这与Alexey Soshin的答案类似,但是语法有所不同。

 var signUserUp = function() { let name = this.state.nameValue; let slackName = this.state.slackNameValue; let email = this.state.emailValue; let password = this.state.passwordValue; firebase.auth().createUserWithEmailAndPassword(email, password).then( function(result){ writeUserData(name, result.uid, email, slackName); }, function(error){ let errorCode = error.code; let errorMessage = error.message; console.log(errorMessage, '***'); console.log(errorCode, 'code'); }); }; var writeUserData = function (name, useruid, email, slackName) { firebase.database().ref('/pairs/' + useruid).set({ name: name, useruid: useruid, email: email, slackName: slackName }); } 

For your second question ("...when I include this.writeUserData ... Cannot read property 'writeUserData' of undefined "): In this case this is undefined. 对于您的第二个问题(“ ...当我包含this.writeUserData ... Cannot read property 'writeUserData' of undefined ):在这种情况下, this是未定义的。

To define this for your second question, you'd have to define your writeUserData inside this . 要为第二个问题定义this ,必须在this定义您的writeUserData

To define writeUserData inside this : 要定义writeUserDatathis

  1. add it on the prototype of the parent function 将其添加到父函数的原型上

     signUserUp.prototype.writeUserData = function(name, useruid, email, slackName) { ... } 
  2. bind the this of the parent function to the child function 将父函数的this绑定到子函数

     firebase.auth().createUserWithEmailAndPassword(email, password).then( ... ).bind(this); 
  3. bind the this of the child function to your function 将子函数的this绑定到您的函数

     function(result) { ... }.bind(this), 

... which is awkward. ...这很尴尬。 So do it the first way! 所以先做吧! :) :)

 var signUserUp = function() { let name = this.state.nameValue; let slackName = this.state.slackNameValue; let email = this.state.emailValue; let password = this.state.passwordValue; signUserUp.prototype.writeUserData = function(name, useruid, email, slackName) { firebase.database().ref('/pairs/' + useruid).set({ name: name, useruid: useruid, email: email, slackName: slackName }); } firebase.auth().createUserWithEmailAndPassword(email, password).then( function(result) { this.writeUserData(name, result.uid, email, slackName); }.bind(this), function(error) { let errorCode = error.code; let errorMessage = error.message; console.log(errorMessage, '***'); console.log(errorCode, 'code'); }).bind(this); }; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM