我在显示图像时遇到问题。
最初图片已成功发布并保存到MongoDb中,您可以在下面的图片中看到
然后从db获取图像,然后显示它们,但是在显示图像时损坏了,并且控制台中显示以下错误。
(注意:-我在前端使用reactjs)
这是从后端获取数据的代码
const getAllPostInfo = async () => {
setLoading();
if (localStorage.token) {
setToken(localStorage.token);
}
try {
const res = await axios({
method: "GET",
url: "/api/post",
});
// console.log(`getting all post`, res.data.status[0].posts);
const resData = res.data.status[0].posts;
dispatch({ type: GET_ALL_POSTS, payload: resData });
} catch (err) {
console.log(err.response);
dispatch({ type: POST_ERRORS, payload: err.response.errors });
}
};
这是减速器动作代码
case GET_ALL_POSTS:
console.log("action payload in post reducer", action.payload);
return {
...state,
post_info: action.payload,
post_loading: false,
errors: null,
};
反应代码-获取单个帖子并显示
const PostItem = ({ post }) => {
const {title,imageUrl,description} =发布; .... }
下面的图片,可以看到帖子被提取了……(在浏览器中,puglin),但是没有显示图片……我不知道我要去哪里错了……
答案 0 :(得分:0)
问题是您的图像存储为文件路径而不是文件名。您需要删除imageUrl数据点中文件名之前的“ images /”。