【问题标题】:Mongodb connection with react native form与反应原生形式的MongoDB连接
【发布时间】:2017-08-28 11:47:23
【问题描述】:

我在 react native 中创建了用户表单,我想使用 mongodb 来存储数据。我已经在我的 mongodb.js 中建立了 mongodb 连接,但是我在将它与我的注册页面连接时遇到了问题。我不想使用 express,因为我的项目很小。

这里是 mongodb.js:

var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://localhost:27017/mytest";

MongoClient.connect(url, function(err, db) {
  if (err) throw err;
  db.createCollection("Users", function(err, res) {
    if (err) throw err;
    console.log("Collection created!");
    db.close();
  });

});

这里是 Register.js:

import React, {Component} from 'react';
import {
  View,
  Text,
  TouchableOpacity,
  TextInput,
  StyleSheet,
  KeyboardAvoidingView,
  Image,
  ScrollView,
  Alert,
} from 'react-native';
import FitImage from 'react-native-fit-image';
import {
  Container,
  Content,
  Header,
  Footer,
  Body,
  FooterTab,
  Icon,
  Card,
  CardItem
} from 'native-base';
import moment from 'moment';
import {FormLabel, FormInput, Button} from 'react-native-elements';
import { DatePickerDialog } from 'react-native-datepicker-dialog';
import HTMLView from 'react-native-htmlview';

    export default class Register extends Component {

  constructor(props){
    super(props);
    this.state = {
      dobText: '',
      dobDate: null,
      journeyText: '',
      journeyDate: null,

      name:'',
      surname:'',
      email:'',
      mobilenumber:'',
      password:'',
      dateofbirth:'',
    }
  }
  onDOBPress = () => {
    let dobDate = this.state.dobDate;

    if(!dobDate || dobDate == null){
      dobDate = new Date();
      this.setState({
        dobDate: dobDate
      });
    }

    //To open the dialog
    this.refs.dobDialog.open({
      date: dobDate,
      maxDate: new Date() //To restirct future date
    });

  }
  onDOBDatePicked = (date) => {
   this.setState({
     dobDate: date,
     dobText: moment(date).format('DD-MMM-YYYY')
   });
 }





 Signup=()=>{
   Alert.alert("You pressed me");
   Alert.alert(this.state.name);
   Alert.alert(this.state.dobText);

 }

  render() {

    return (

      <View>
        <ScrollView>
          <Card>
            <CardItem>
            <Content>



        <KeyboardAvoidingView behavior="padding" >
          <FormLabel>Name</FormLabel>
          <FormInput onChangeText={name=>this.setState({name})}/>
          <FormLabel>Surname</FormLabel>
          <FormInput onChangeText={surname=>this.setState({surname})}/>
          <FormLabel>Date of Birth</FormLabel>
          <TouchableOpacity onPress={this.onDOBPress.bind(this)} onChangeText=
        {dateofbirth=>this.setState({dateofbirth})}>
            <View style={styles.datePickerBox}>
              <Text style={styles.datePickerText}>{this.state.dobText}</Text>
              <DatePickerDialog ref="dobDialog" onDatePicked={this.onDOBDatePicked.bind(this)} />
            </View>
          </TouchableOpacity>


          <FormLabel>Email</FormLabel>
          <FormInput onChangeText={email=>this.setState({email})}/>
          <FormLabel>Mobile Number</FormLabel>
          <FormInput onChangeText={mobilenumber=>this.setState({mobilenumber})}/>
          <FormLabel>Password</FormLabel>
          <FormInput secureTextEntry onChangeText={password=>this.setState({password})}/>
          <TouchableOpacity >
            <Button
              icon={{name: 'send'}}
              title='Submit Details'
              backgroundColor="#4286f4"
              onPress={this.Signup}
            />
          </TouchableOpacity>
        </KeyboardAvoidingView>
        </Content>
      </CardItem>

      </Card>
    </ScrollView>
  </View>

);

  }

}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: 'green'
  },
  Label:{
    fontSize: 20,
  },
  form: {
    alignSelf: 'stretch',
    backgroundColor: 'grey',
    justifyContent: 'center',
    alignItems: 'center',
  },
  TextStyle: {
    width: 250,
    justifyContent: 'center',
    alignItems: 'center',
    textAlign: 'center',
    color: 'lightgreen'
  },
  TouchStyle: {
    width: 250,
    backgroundColor: 'blue',
    justifyContent: 'center',
    marginTop: 20
  },
  TouchText: {
    alignItems: 'center',
    textAlign: 'center',
    justifyContent: 'center',
    fontSize: 20,
    color: 'white'
  },
  content: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  datePickerBox:{
   marginTop: 9,
   borderColor: '#ABABAB',
   borderWidth: 0.5,
   padding: 0,
   borderTopLeftRadius: 4,
   borderTopRightRadius: 4,
   borderBottomLeftRadius: 4,
   borderBottomRightRadius: 4,
   height: 28,
   justifyContent:'center'
 },
  datePickerText: {
    fontSize: 14,
    marginLeft: 5,
    borderWidth: 0,
    color: '#121212',
  },
});

请告诉我怎么做,任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 您在日志中遇到什么错误?您还必须通过进入终端(我有一个 mac)并输入 mongod 来确保您已连接到您的 mongo。
  • 这与错误无关。我已经通过进入终端与 mongo 建立了联系。实际上我无法将它与用户表单(即registration.js 页面)连接起来。每当我尝试在registration.js中导入(从'./Mongodb'导入Mongodb)时都会出错。如果我在我的 registration.js 中编写 MongoClient 它也会给出错误。我的问题是在这种情况下如何在 mongodb 中插入值?
  • @DroiDev 先生你介意告诉我怎么做吗?

标签: javascript node.js mongodb react-native


【解决方案1】:

我认为你应该使用你的机器 IP 而不是 localhost。 它应该是这样的。

var url = "mongodb://192.168.8.5:27017/mytest";

如果不起作用,请使用httphttps 而不是mongodb

var url = "http://192.168.8.5:27017/mytest";

var url = "https://192.168.8.5:27017/mytest";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-01
    • 2021-10-25
    • 2019-05-18
    • 1970-01-01
    • 2018-02-18
    • 1970-01-01
    相关资源
    最近更新 更多