无法读取未定义的属性“用户”(React-Native)

时间:2018-11-02 18:46:57

标签: javascript reactjs react-native

即使正在毫无问题地创建用户,我也得到了Cannot read property 'user' of undefined的帮助,当我点击register时,它不会将我导航到主页,我检查了路线,并且所有名称都相同。 Register.js在哪里给我错误:

  

Register.js:50类型错误:类型错误:类型错误:无法读取属性   未定义的“用户”

Register.js:

import React, { Component } from "react";
import {
  View,
  Text,
  TouchableOpacity,
  TextInput,
  StyleSheet
} from "react-native";
import config from "../../config";
class Register extends Component {
  constructor() {
    super();
    this.state = {
      credenciais: {
        email: "",
        password: ""
      }
    };
  }
  updateText(text, field) {
    let newCredenciais = Object.assign(this.state.credenciais);
    newCredenciais[field] = text;
    this.setState({
      credenciais: newCredenciais
    });
  }

  register() {
    fetch(config.baseUrl + "signup", {
      method: "POST",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json"
      },
      body: JSON.stringify(this.state.credenciais)
    })
      .then(response => response.json())
      .then(jsonResponse => {
        if (jsonResponse.confirmation === "success") {
          this.props.navigation.navigate("main");
        } else {
          throw new Error({
            message: "Algo ocorreu de errado, por favor tente novamente"
          });
        }
      })
      .catch(err => {
        console.log(err.message);
      });

  }

  render() {
    return (
      <View
        style={{
          height: 100 + "%",
          width: 100 + "%",
          flex: 1,
          justifyContent: "center",
          alignItems: "center"
        }}
      >

        <TextInput
          value={this.state.email}
          onChangeText={text => this.updateText(text, "email")}
          placeholder="Email"
          style={styles.input}
          autoCorrect={false}
        />
        <TextInput
          autoCorrect={false}
          value={this.state.password}
          onChangeText={text => this.updateText(text, "password")}
          secureTextEntry
          placeholder="Senha"
          style={styles.input}
        />
        <TouchableOpacity
          onPress={() => {
            this.register();
          }}
        >
          <Text style={{ color: "rgb(80,166,255)" }}>Registrar</Text>
        </TouchableOpacity>
      </View>
    );
  }
}
const styles = StyleSheet.create({
  input: {
    height: 40,
    width: 100 + "%",
    backgroundColor: "rgb(200,200,200)",
    paddingHorizontal: 20,
    marginTop: 10,
    marginBottom: 10
  }
});
export default Register;

和api路由:

// Full Documentation - https://www.turbo360.co/docs
const turbo = require("turbo360")({ site_id: process.env.TURBO_APP_ID });
const vertex = require("vertex360")({ site_id: process.env.TURBO_APP_ID });
const router = vertex.router();

/*  This is a sample API route. */

router.get("/:resource", function(req, res) {
  const { resource } = req.params;
  const { query } = req;
  turbo
    .fetch(resource, query)
    .then(data => {
      res.json({
        confirmation: "success",
        data: data
      });
      return;
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        message: "Sorry, something went wrong"
      });
      return;
    });
});

router.post("/signup", function(req, res) {
  console.log(req.body);
  turbo
    .createUser(req.body)
    .then(data => {
      res.json({
        confirmation: "success",
        data: data
      });
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        message: err.message
      });
    });
});

router.post("/login", function(req, res) {
  console.log(req.body);
  turbo
    .login(req.body)
    .then(data => {
      res.json({
        confirmation: "success",
        data: data
      });
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        message: err.message
      });
    });
});

router.post("/login", function(req, res) {
  console.log(req.body);
  turbo
    .login(req.body)
    .then(data => {
      res.json({
        confirmation: "success",
        data: data
      });
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        message: err.message
      });
    });
});

router.get("/users/:id/photo", function(req, res) {
  const userId = req.params.id;
  console.log(req.params);
  const myPhoto = {
    url: req.body.imageUrl,
    user: req.params.id
  };
  turbo
    .create("photo", myPhoto)
    .then(data => {
      res.json({
        confirmation: "success",
        data: data
      });
      return;
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        message: "Sorry, something went wrong"
      });
      return;
    });
});

router.post("/users/:id/photo", function(req, res) {
  const userId = req.params.id;
  console.log(req.params);
  const myPhoto = {
    url: req.body.imageUrl,
    user: req.params.id
  };
  turbo
    .create("photo", myPhoto)
    .then(resp => {
      res.json({
        confirmation: "success",
        data: resp
      });
    })
    .catch(err => {
      res.json({
        confirmation: "fail",
        err: err
      });
    });
});

router.get("/:resource/:id", function(req, res) {
  res.json({
    confirmation: "success",
    resource: req.params.resource,
    id: req.params.id,
    query: req.query // from the url query string
  });
});

module.exports = router;

所有这一切的怪异之处在于,它仍在注册并保存用户数据,然后我可以重新加载该应用程序并仅使用导致该错误的详细信息登录。 我正在使用react-native,turbo360作为api和react-navigation

0 个答案:

没有答案