本文将带您了解关于如何使用ReactNative在MERN堆栈中将图像上传到MongoDB?的新内容,同时我们还将为您解释reactnative上传图片的相关知识,另外,我们还将为您提供关于javas
本文将带您了解关于如何使用 React Native 在 MERN 堆栈中将图像上传到 MongoDB?的新内容,同时我们还将为您解释react native 上传图片的相关知识,另外,我们还将为您提供关于javascript – 将图像从React-Native应用程序上传到Firebase存储、MongoDB 与 Expo React Native、NextJS-具有react-native-elements的React-Native monorepo、node.js – 使用Multer将图像上传到Express上传的实用信息。
本文目录一览:- 如何使用 React Native 在 MERN 堆栈中将图像上传到 MongoDB?(react native 上传图片)
- javascript – 将图像从React-Native应用程序上传到Firebase存储
- MongoDB 与 Expo React Native
- NextJS-具有react-native-elements的React-Native monorepo
- node.js – 使用Multer将图像上传到Express上传
如何使用 React Native 在 MERN 堆栈中将图像上传到 MongoDB?(react native 上传图片)
首先使用它们的 API 将图像上传到存储服务,例如 AWS S3 或 Firebase 存储。 API 返回上传图像的 URL 后,您可以在 mongodb 文档中更新该 URL。
javascript – 将图像从React-Native应用程序上传到Firebase存储
var file = { uri: 'file:///Users/...../LONG_PATH/....../Documents/images/name.jpg' } var storageRef = this.firebase.storage().ref(); var uploadTask = storageRef.child('images/' + file.name).put(file);
但它抛出一个错误:
Possible Unhandled Promise Rejection (id: 0):
Firebase Storage:
Invalid argument input
at index 0: Can’t find variable: Blob
解决方法
React Native does not support the File and Blob types,so Firebase Storage uploads will not work in this environment. File downloads do work however.
但我发现这个module可以将文件转换为blob类型. Maibe可以为您的解决方案提供帮助
MongoDB 与 Expo React Native
如何解决MongoDB 与 Expo React Native?
我一直在尝试弄清楚我的 React Native 项目要使用哪个数据库(使用 Expo)。我最终需要在线功能,但更愿意在本地开发。我最初在看 MongoDB/Realm,但两者似乎都不支持 expo。这样对吗?或者有没有办法在 React Native 和 Expo 中使用 MongoDB?
解决方法
我认为您可以使用您喜欢的任何数据库,方法是在您的本机项目中使用 fetch 通过 REST API 连接到它,如果您想实现离线和实时功能,我建议您查看 firestore 或实时数据库
NextJS-具有react-native-elements的React-Native monorepo
如何解决NextJS-具有react-native-elements的React-Native monorepo?
我正在尝试在本机单反应源中使用本机元素。 它在Native App中工作正常,但在NextJS应用程序中却无法正常工作。
首先,它会成功编译:
> with-react-native-web@ dev <path>\WebApp
> next
ready - started server on http://localhost:3000
info - automatically enabled Fast Refresh for 1 custom loader
info - Using external babel configuration from <path>\WebApp\babel.config.js
event - compiled successfully
但是当我尝试访问http:// localhost:3000时,它再次开始编译,但是失败:
<path>\WebApp\node_modules\react-native-elements\src\index.js:7
import Button from ''./buttons/Button'';
^^^^^^
SyntaxError: Cannot use import statement outside a module
at wrapSafe (internal/modules/cjs/loader.js:1001:16)
at Module._compile (internal/modules/cjs/loader.js:1049:27)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
at Module.load (internal/modules/cjs/loader.js:950:32)
at Function.Module._load (internal/modules/cjs/loader.js:791:14)
at Module.require (internal/modules/cjs/loader.js:974:19)
at require (internal/modules/cjs/helpers.js:88:18)
at Object.react-native-elements (<path>\WebApp\.next\server\pages\_app.js:3783:18)
at __webpack_require__ (<path>\WebApp\.next\server\pages\_app.js:23:31)
at Module../pages/_app.js (<path>\WebApp\.next\server\pages\_app.js:3640:79)
我认为该错误可能在next.config.js文件中:
const path = require(''path'');
module.exports = {
webpack: (config,{ defaultLoaders }) => {
config.resolve = {
...config.resolve,alias: {
...config.resolve.alias,''react-native'': path.join(__dirname,''node_modules'',''react-native-web''),},modules: [
...config.resolve.modules,path.resolve(__dirname,''node_modules''),],}
config.module.rules.push({
test: /\.+(js|jsx)$/,use: defaultLoaders.babel,include: [
path.resolve(__dirname,''..'',''shared''),''react-native-elements''),''react-native-vector-icons''),''react-native-ratings''),})
config.module.rules.push({
test: /\.(jpe?g|png|gif|svg)$/i,loader: "file-loader?name=/public/icons/[name].[ext]"
})
return config;
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
node.js – 使用Multer将图像上传到Express上传
server.js
var storage = multer.diskStorage({ destination: './public/users',filename: function (req,file,cb) { switch (file.mimetype) { case 'image/jpeg': ext = '.jpeg'; break; case 'image/png': ext = '.png'; break; } cb(null,file.originalname + ext); } }); var upload = multer({storage: storage}); app.use(upload.single('photo')); app.post('/uploadUserImage',function (req,res) { console.log(JSON.stringify(req.body.photo)) // form fields console.log(req.photo) // form files console.log(req.file) // form files res.send(req.body.photo); });
client.js
function uploadImage (image) { var formData = new FormData(); formData.append('photo',image); fetch('http://localhost:8080/uploadUserImage/',{ method:'POST',body: formData }); }
当我提出这个请求时,摩根会注销以下内容:
{photo:'[object File]’}< ---来自console.log(req.body'); undefined< ---来自console.log(req.file); multer创建文件夹public / uploads但不将图像放在该位置.如何获取照片,因为我需要在sharp中运行它(压缩文件大小并调整图像大小),然后将其放在uploads文件夹中?
解决方法
要解决文件上载的问题,您应该从获取请求中删除“Content-Type”规范.您还可以重构uploadImage方法以上载表单而无需解析输入:
function uploadImage () { // This assumes the form's name is `myForm` var form = document.getElementById("myForm"); var formData = new FormData(form); fetch('http://localhost:8000/uploadUserImage',{ method: 'POST',body: formData }); }
我们今天的关于如何使用 React Native 在 MERN 堆栈中将图像上传到 MongoDB?和react native 上传图片的分享就到这里,谢谢您的阅读,如果想了解更多关于javascript – 将图像从React-Native应用程序上传到Firebase存储、MongoDB 与 Expo React Native、NextJS-具有react-native-elements的React-Native monorepo、node.js – 使用Multer将图像上传到Express上传的相关信息,可以在本站进行搜索。
本文标签: