React native 使用axios从GridFS下载图像(React Native)
我的NodeJS服务器中有以下功能,可从mongoDB GridFS文件存储中提取图像:React native 使用axios从GridFS下载图像(React Native),react-native,gridfs,React Native,Gridfs,我的NodeJS服务器中有以下功能,可从mongoDB GridFS文件存储中提取图像: router.get("/photo", (req, res) => { const { id } = req.query; const file = gfs .find({ _id: ObjectId(id) }) .toArray((err, files) => { if (!
router.get("/photo", (req, res) => {
const { id } = req.query;
const file = gfs
.find({ _id: ObjectId(id) })
.toArray((err, files) => {
if (!files || files.length === 0) {
return res.status(404).json({
err: "no files exist"
});
}
gfs.openDownloadStream(ObjectId(id)).pipe(res);
});
});
它在邮递员身上完美地工作,图像显示在上面。然而,我试图在我的react原生应用程序中下载并向用户显示,但我从服务器获得的所有信息都是一个空的数组缓冲区:
const getAvatar = dispatch => (customer) => {
console.log(customer.avatar)
dispatch({ type: "FETCH_AVATAR" });
try {
// axios instance
server.get(`/api/customer/photo?id=${customer.avatar}`, { responseType: 'arraybuffer' }).
then(res => {
console.log(res.data);
})
//console.log(response);
} catch (error) {
const { response: { data } } = error;
dispatch({ type: 'FETCH_AVATAR_REJECTED', payload: data });
}
}
知道我做错了什么吗