微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

显示数据库中的数据 (MERN)

如何解决显示数据库中的数据 (MERN)

我使用multer将照片添加到MongoDB数据库中,现在我想显示它们,控制台数据如下所示:

enter image description here

代码前端应用:

constructor(props){
        super(props);

        this.onFileChange = this.onFileChange.bind(this);
        this.onSubmit = this.onSubmit.bind(this);

        this.state = {
            profileImg: '',image:[]
            
        }
    }



  async fetchImage(){
        var a = await axios.get('http://localhost:3001/api');
        const img = a.data;
        this.setState({text:[img.users]})
        console.log("Dane stanu: " + this.state.image)
    }

And i try to map this:

{this.state.image.map(person => <li key={person._id}><img src={person.profileImg} alt="błąd"/></li>)}

但是什么都没显示,请帮我找到错误

编辑:我在 componentDidMount() 中添加了 fetchImage()

 componentDidMount()
    {
        this.fetchImage();
    }


解决方法

我以这种方式将图像数据从服务器发送到客户端:

fs.readFile(slidePath,(err,buf) => {
client.send(JSON.stringify({img: buf.toString('base64')})]

在反应客户端上,我是这样理解的:

<img src=`data:image/jpg;base64,${postData.img}`>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。