Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Can';t显示MongoDB中的内容,给我空数组[]_Javascript_Html_Node.js_Reactjs_Mongodb - Fatal编程技术网

Javascript Can';t显示MongoDB中的内容,给我空数组[]

Javascript Can';t显示MongoDB中的内容,给我空数组[],javascript,html,node.js,reactjs,mongodb,Javascript,Html,Node.js,Reactjs,Mongodb,我试图显示一个数组,即来自MongoDB的列表,但找不到我犯错误的位置,因为当我转到我启动服务器的位置时,它给出了一个空[]。我还使用了cores中间件,因为我将后端和前端分开,所以前端可以随时访问后端。我的UI是用React制作的。用于后端的NodeJS 这是我的后端 const express = require("express"); const mongoose = require("mongoose"); const app = express(); app.use(function

我试图显示一个数组,即来自MongoDB的列表,但找不到我犯错误的位置,因为当我转到我启动服务器的位置时,它给出了一个空[]。我还使用了cores中间件,因为我将后端和前端分开,所以前端可以随时访问后端。我的UI是用React制作的。用于后端的NodeJS

这是我的后端

const express = require("express");
const mongoose = require("mongoose");
const app = express();

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

const bodyParser = require("body-parser");

app.use(bodyParser.json());

mongoose
  .connect("mongodb://localhost:27017/infos")
  .then(() => console.log("Connected to MongoDB..."))
  .catch(err => console.error("Could not connect to MongoDB"));

const Infos = mongoose.model(
  "infos",
  new mongoose.Schema({
    name: {
      type: String,
      required: true
    },
    email: {
      type: String,
      required: true
    },
    phone: {
      type: String,
      required: true
    }
  })
);

let router = express.Router();

router.get("/infos", (req, res) => {
  Infos.find((err, result) => {
    console.log(err);
    console.log(result);
    res.send(result);
  });
});

app.use(router);

const Port = process.env.PORT || 4000;

app.listen(Port, () =>
  console.log(`Server started at http://localhost:${Port}`)
);

module.exports = Infos;
import React, { Component } from "react";
import axios from "axios";

class Infos extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: []
    };
  }

  render() {
    return (
      <React.Fragment>
        <div className="container">
          <div className="row">
            {this.state.items.map(el => {
              return (
                <div className="col">
                  <div>
                    <h3>{el.name}</h3>
                    <h3>{el.email}</h3>
                    <h3>{el.phone}</h3>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </React.Fragment>
    );
  }

  componentDidMount() {
    axios.get("http://localhost:4000/infos").then(res => {
      this.setState({
        items: res.data
      });
    });
  }
}

export default Infos;
这是我的前端

const express = require("express");
const mongoose = require("mongoose");
const app = express();

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header(
    "Access-Control-Allow-Headers",
    "Origin, X-Requested-With, Content-Type, Accept"
  );
  next();
});

const bodyParser = require("body-parser");

app.use(bodyParser.json());

mongoose
  .connect("mongodb://localhost:27017/infos")
  .then(() => console.log("Connected to MongoDB..."))
  .catch(err => console.error("Could not connect to MongoDB"));

const Infos = mongoose.model(
  "infos",
  new mongoose.Schema({
    name: {
      type: String,
      required: true
    },
    email: {
      type: String,
      required: true
    },
    phone: {
      type: String,
      required: true
    }
  })
);

let router = express.Router();

router.get("/infos", (req, res) => {
  Infos.find((err, result) => {
    console.log(err);
    console.log(result);
    res.send(result);
  });
});

app.use(router);

const Port = process.env.PORT || 4000;

app.listen(Port, () =>
  console.log(`Server started at http://localhost:${Port}`)
);

module.exports = Infos;
import React, { Component } from "react";
import axios from "axios";

class Infos extends Component {
  constructor(props) {
    super(props);
    this.state = {
      items: []
    };
  }

  render() {
    return (
      <React.Fragment>
        <div className="container">
          <div className="row">
            {this.state.items.map(el => {
              return (
                <div className="col">
                  <div>
                    <h3>{el.name}</h3>
                    <h3>{el.email}</h3>
                    <h3>{el.phone}</h3>
                  </div>
                </div>
              );
            })}
          </div>
        </div>
      </React.Fragment>
    );
  }

  componentDidMount() {
    axios.get("http://localhost:4000/infos").then(res => {
      this.setState({
        items: res.data
      });
    });
  }
}

export default Infos;
import React,{Component}来自“React”;
从“axios”导入axios;
类Infos扩展组件{
建造师(道具){
超级(道具);
此.state={
项目:[]
};
}
render(){
返回(
{this.state.items.map(el=>{
返回(
{el.name}
{el.email}
{el.phone}
);
})}
);
}
componentDidMount(){
axios.get(“http://localhost:4000/infos)然后(res=>{
这是我的国家({
项目:资源数据
});
});
}
}
导出默认信息;
我还在MongoDB中创建了带有姓名、电子邮件和电话的“infos”数据库