简体   繁体   English

ReactJS-如何从json服务器迁移到.json文件?

[英]ReactJS - How to migrate from json-server to a .json file?

I have this db.json and I want to migrate to file. 我有这个db.json,我想迁移到文件。 How do I implement it? 如何实施? I had tried this way above and it didn't work for me. 我在上面已经尝试过这种方法,但对我却不起作用。 When I put the file in src folder, there appears an error message: 将文件放在src文件夹中时,出现错误消息:

Module not found: Can't resolve 'houses.json' in 'C:\\Users\\user\\Desktop\\dir\\src' 找不到模块:无法解析“ C:\\ Users \\ user \\ Desktop \\ dir \\ src”中的“ houses.json”

It does the same thing when I put the file in the public folder. 当我将文件放在公用文件夹中时,它执行相同的操作。

It is my db.json file: 这是我的db.json文件:

 {   "houses": [
     {
       "id": 1,
       "name": "Title here",
       "text": "Text here",
       "photos": [
         "pic1",
         "pic2",
         "pic3"        
      ]
     },
     "houses": [
     {
       "id": 2,
       "name": "Title here",
       "text": "Text here",
       "photos": [
         "pic1",
         "pic2",
         "pic3"        
       ]
     },
     "houses": [
     {
       "id": 3,
       "name": "Title here",
       "text": "Text here",
       "photos": [
         "pic1",
         "pic2",
         "pic3"        
       ]
     } 
 }   

I had traded this line 我已经交易了这条线

const URL = 'http://localhost:3001/houses';

for this other 为了这个

 const URL = require('houses.json');

And it caused the error message showed before. 并导致了之前显示的错误消息。

And how do I fetch these datas from axios? 以及如何从axios提取这些数据? I was fetching datas from json-server doing this way below. 我在下面以这种方式从json服务器获取数据。 It was successfully. 成功了。 I want to do the same thing but not from json-server but using .json file. 我想做同样的事情,但不是从json-server而是使用.json文件。

const URL = 'http://localhost:3001/houses';

class House extends Component {

  constructor(props) {
    super(props);
    this.state = {
      index: 0,
      totalHouses: [],
      currentHouse: [],
      name: [],
      photos: [],
      text: []
    };
  }

  componentDidMount() {
    axios.get(URL)
      .then(res => {
        this.setState({
          totalHouses: Object.keys(res.data),
          currentHouse: res.data
        })
      })
  }

//...rest of the code omitted

Here's a simple example how to achieve what you need, while preserving your state approach. 这是一个简单的示例,如何在保留状态方法的同时实现所需的功能。

const houses = require('./houses.json');

class House extends Component {

  constructor(props) {
    super(props);
    this.state = {
      index: 0,
      totalHouses: [],
      currentHouse: [],
      name: [],
      photos: [],
      text: []
    };
  }

  componentDidMount() {
    this.setState({
      totalHouses: Object.keys(houses.data),
      currentHouse: houses.data
    })
  }

  //...

}

You can also achieve it without componentDidMount : 您也可以不使用componentDidMount实现它:

const houses = require('./houses.json');

class House extends Component {

  constructor(props) {
    super(props);
    this.state = {
      index: 0,
      totalHouses: Object.keys(houses.data),
      currentHouse: houses.data
      name: [],
      photos: [],
      text: []
    };
  }

  //...

}

It should be require('./houses.json'); 应该是require('./houses.json'); . Right now it looks for this file inside node_modules or even higher. 现在,它将在node_modules或更高版本中查找此文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM