我将图像以base64格式存储在节点中,然后接收并存储在变量中,并在标记中显示,但它没有显示。从服务器接收到正确的值。在渲染函数中,如果状态已设置,则条件为真。即使为真,它也不会显示。
getImage() {
console.log('getImage');
axios.get(`http://localhost:4000/image/get`).then((result) => {
this.setState({ image: result })
console.log(result);
});
}
render(){
{this.state.image ? <img src={this.state.image}></img>: ''}
}
我得到了确切的Base64字符串并将其存储在服务器中。它返回:
<img src="[object Object]">
在 DOM 中,我不知道自己哪里出错了。
编辑
router.route('/image/get').get((req, res) => {
console.log('inside img get');
Image.find((err, result) => {
if (err) {
res.json({ "error": true, "message": "error fetching data" });
} else {
// console.log(result);
res.json(result);
}
})
});
模型
const mongoose=require('mongoose');
const Schema=mongoose.Schema;
var ImageSchema=new Schema({
imageName:{
type:String
},
imageData:{
type:String
}
});
export default mongoose.model('Image', ImageSchema);