Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.19 KB | None | 0 0
  1. var firebase = require('firebase');
  2.  
  3. if (!firebase.apps.length) {
  4. firebase.initializeApp({
  5. apiKey: "key",
  6. authDomain: "domain",
  7. databaseURL: "url",
  8. storageBucket: "bucket",
  9. });
  10. }
  11.  
  12. class Database {
  13. constructor() {
  14. this.codesRef = firebase.database().ref('codes');
  15. }
  16. isValidCode(text) {
  17. let codeIsFound = false;
  18. let identifier = "";
  19. this.codesRef.once('value', (db_snapshot) => {
  20. db_snapshot.forEach((code_snapshot) => {
  21. //console.log(text, code_snapshot.val().value, text == code_snapshot.val().value);
  22. if (text == code_snapshot.val().value) {
  23. codeIsFound = true;
  24. identifier = code_snapshot.key;
  25. }
  26. });
  27. //console.log(codeIsFound); // this is correct
  28. return codeIsFound; // this always returns undefined
  29. });
  30. };
  31. }
  32.  
  33. module.exports = Database;
  34.  
  35. import React from 'react';
  36. import {
  37. StyleSheet,
  38. View,
  39. TextInput,
  40. } from 'react-native';
  41.  
  42. import { withNavigation } from 'react-navigation';
  43.  
  44. import database from '../Database.js';
  45.  
  46. const db = new database();
  47.  
  48.  
  49. class LoginForm extends React.Component {
  50. constructor(props) {
  51. super(props);
  52. }
  53.  
  54. render() {
  55. return (
  56. <View style={styles.container}>
  57. <TextInput
  58. style={styles.input}
  59. placeholder="Access Code"
  60. returnKeyType="go"
  61. onSubmitEditing={text => {console.log(db.validCode(text.nativeEvent.text))}} // "undefined"
  62. autoCapitalize="none"
  63. autoCorrect={false}
  64. />
  65. </View>
  66. );
  67. }
  68. }
  69.  
  70. const styles = StyleSheet.create({
  71. // yay styles :)
  72. });
  73.  
  74. export default withNavigation(LoginForm);
  75.  
  76. isValidCode(text) {
  77. let codeIsFound = false;
  78. let identifier = "";
  79. this.codesRef.once('value', (db_snapshot) => {
  80. db_snapshot.forEach((code_snapshot) => {
  81. //console.log(text, code_snapshot.val().value, text == code_snapshot.val().value);
  82. if (text == code_snapshot.val().value) {
  83. codeIsFound = true;
  84. identifier = code_snapshot.key;
  85. takeValue(codeIsFound);
  86. }
  87. });
  88. });
  89. };
  90.  
  91. takeValue(value){
  92. console.log(value) //true
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement