如何解决React Native中的``未定义不是对象''错误

时间:2019-05-19 02:18:12

标签: javascript firebase react-native ecmascript-6 firebase-authentication

我正在尝试将我的本机应用程序代码移到更结构化的方式。最初,我将所有firebase函数都放在使用它们的文件中,但是现在我想在多个地方使用它们,因此我创建了一个 Database.js 文件,其中包含一个Database类和所有功能。但是由于某种原因,每当我尝试使用新类中的函数之一时,都会收到错误"undefined is not an object (evaluating 'this.codesRef.once')",请帮忙!

到目前为止,我尝试使用箭头函数,构造函数,并以不同方式导入firebase,但都无济于事。我对此非常困惑。

看看代码... (/ project / src / components / forms / KeyForm.js)

import React from 'react';
import { StyleSheet, View, TextInput } from 'react-native';

import db from '../Database.js';

class LoginForm extends React.Component {
  constructor(props) {
    super(props);
  }

  render() {
    return (
      <View style={styles.container}>
        <TextInput
          placeholder="Access Code"
          returnKeyType="go"
          onSubmitEditing={text => {db.checkCode(text.nativeEvent.text)}}
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({ // stylesheet
  // yay styles :)
});

export default LoginForm;

(/ project / src / components / Database.js)

//import * as firebase from "firebase";
var firebase = require('firebase');

if (!firebase.apps.length) {
    firebase.initializeApp({
        apiKey: "key",
        authDomain: "domain",
        databaseURL: "url",
        storageBucket: "bucket",
    });
}

class Database {

    codesRef = firebase.database().ref('codes');

    static checkCode(text) {
        let codeIsFound = false;
        this.codesRef.once('value', (db_snapshot) => { // this won't work
          db_snapshot.forEach((code_snapshot) => {
            if (text == code_snapshot.val().value) {
              codeIsFound = true;
              identifier = code_snapshot.key;
            }
          });
        });
        if (codeIsFound) {
            //this.deleteCode(identifier);
            console.log("code found");
            this.props.navigation.navigate('Create'); // side-question => how can i get this working in Database.js? Do i need to use withNavigation?
          } else {
            console.log("code not found");
            );
          }
    };
}

module.exports = Database;

请澄清一下,在我尝试将函数迁移到Database.js文件之前,一切都可以100%正常工作。任何帮助将不胜感激!

3 个答案:

答案 0 :(得分:2)

您的checkCode函数是static。您无法在静态方法内访问this上下文。 在您的 /project/src/components/Database.js 中将其更改为:

checkCode(text) {
        let codeIsFound = false;
        this.codesRef.once('value', (db_snapshot) => { // this won't work
          db_snapshot.forEach((code_snapshot) => {
            if (text == code_snapshot.val().value) {
              codeIsFound = true;
              identifier = code_snapshot.key;
            }
          });
        });
        if (codeIsFound) {
            //this.deleteCode(identifier);
            console.log("code found");
            this.props.navigation.navigate('Create'); // side-question => how can i get this working in Database.js? Do i need to use withNavigation?
          } else {
            console.log("code not found");
            );
          }
    };

/project/src/components/forms/KeyForm.js

中访问此函数时
import firbaseDB from '../Database.js';
const db = new firbaseDB();
...

按原样保留您的代码。干杯。

答案 1 :(得分:1)

尝试在您的类中使用构造函数:

class Database {
    constructor() {
        this.codesRef = firebase.database().ref("codes");
    }
    //...
}

答案 2 :(得分:0)

也许您必须这样做

class Database {
    constructor(props) {
       super(props);
        this.codesRef = firebase.database().ref("codes");
    }
    //...
}