How to upload Image to MongoDB and string data, simultaneously, using GridFS?
Am trying to create an app to upload and caption images, directly to mongodb using GridFs. And then create an API to show these images and caption at client side.
Major Problem.
This is my multer with grdifs storage engine. Works like a charm.
const storage = new GridFsStorage({
url : mongoURI,
file : (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buff) => {
if(err){
return reject(err);
}
const filename = buff.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
caption : req.body.name,
bucketName : 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.post('/upload', upload.single('file'), (req, res) =>{
res.json({file : req.file});
});
The problem is, when I try to incorporate another action using a Post Model I created, using mongoose scheme, on this route, after uploading the image,like so.
app.post('/upload', upload.single('file'), (req, res) =>{
const newPost = new Post({
caption : req.body.caption,
image : req.file.filename
})
newPost.save()
.then(post => res.json('Posted'))
.catch(err => console.log(err)) ;
//res.json({file : req.file});
});
The server just gets stuck on loading.
So, my question is, does using gridfs, change how we normally use mongoose models with MongoDB?
My Post model (post.js)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Post Schema
let PostSchema = new Schema({
caption : {
type : String,
required : true
},
image : {
type: String,
required: true
}
});
let Post = module.exports = mongoose.model('Post', PostSchema);
I want to use this Post collection, to store the image's filename that was uploaded, along with caption from client. Am doing something wrong, and I need help.
Another problem, right now am using a form to make this POST request. But when I try to use axios to make a post request to my API port(i.e 5000) from my client port(i.e 8080), I get CORS error.
How do I overcome this?
node.js reactjs mern
add a comment |
Am trying to create an app to upload and caption images, directly to mongodb using GridFs. And then create an API to show these images and caption at client side.
Major Problem.
This is my multer with grdifs storage engine. Works like a charm.
const storage = new GridFsStorage({
url : mongoURI,
file : (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buff) => {
if(err){
return reject(err);
}
const filename = buff.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
caption : req.body.name,
bucketName : 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.post('/upload', upload.single('file'), (req, res) =>{
res.json({file : req.file});
});
The problem is, when I try to incorporate another action using a Post Model I created, using mongoose scheme, on this route, after uploading the image,like so.
app.post('/upload', upload.single('file'), (req, res) =>{
const newPost = new Post({
caption : req.body.caption,
image : req.file.filename
})
newPost.save()
.then(post => res.json('Posted'))
.catch(err => console.log(err)) ;
//res.json({file : req.file});
});
The server just gets stuck on loading.
So, my question is, does using gridfs, change how we normally use mongoose models with MongoDB?
My Post model (post.js)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Post Schema
let PostSchema = new Schema({
caption : {
type : String,
required : true
},
image : {
type: String,
required: true
}
});
let Post = module.exports = mongoose.model('Post', PostSchema);
I want to use this Post collection, to store the image's filename that was uploaded, along with caption from client. Am doing something wrong, and I need help.
Another problem, right now am using a form to make this POST request. But when I try to use axios to make a post request to my API port(i.e 5000) from my client port(i.e 8080), I get CORS error.
How do I overcome this?
node.js reactjs mern
add a comment |
Am trying to create an app to upload and caption images, directly to mongodb using GridFs. And then create an API to show these images and caption at client side.
Major Problem.
This is my multer with grdifs storage engine. Works like a charm.
const storage = new GridFsStorage({
url : mongoURI,
file : (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buff) => {
if(err){
return reject(err);
}
const filename = buff.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
caption : req.body.name,
bucketName : 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.post('/upload', upload.single('file'), (req, res) =>{
res.json({file : req.file});
});
The problem is, when I try to incorporate another action using a Post Model I created, using mongoose scheme, on this route, after uploading the image,like so.
app.post('/upload', upload.single('file'), (req, res) =>{
const newPost = new Post({
caption : req.body.caption,
image : req.file.filename
})
newPost.save()
.then(post => res.json('Posted'))
.catch(err => console.log(err)) ;
//res.json({file : req.file});
});
The server just gets stuck on loading.
So, my question is, does using gridfs, change how we normally use mongoose models with MongoDB?
My Post model (post.js)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Post Schema
let PostSchema = new Schema({
caption : {
type : String,
required : true
},
image : {
type: String,
required: true
}
});
let Post = module.exports = mongoose.model('Post', PostSchema);
I want to use this Post collection, to store the image's filename that was uploaded, along with caption from client. Am doing something wrong, and I need help.
Another problem, right now am using a form to make this POST request. But when I try to use axios to make a post request to my API port(i.e 5000) from my client port(i.e 8080), I get CORS error.
How do I overcome this?
node.js reactjs mern
Am trying to create an app to upload and caption images, directly to mongodb using GridFs. And then create an API to show these images and caption at client side.
Major Problem.
This is my multer with grdifs storage engine. Works like a charm.
const storage = new GridFsStorage({
url : mongoURI,
file : (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buff) => {
if(err){
return reject(err);
}
const filename = buff.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
caption : req.body.name,
bucketName : 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });
app.post('/upload', upload.single('file'), (req, res) =>{
res.json({file : req.file});
});
The problem is, when I try to incorporate another action using a Post Model I created, using mongoose scheme, on this route, after uploading the image,like so.
app.post('/upload', upload.single('file'), (req, res) =>{
const newPost = new Post({
caption : req.body.caption,
image : req.file.filename
})
newPost.save()
.then(post => res.json('Posted'))
.catch(err => console.log(err)) ;
//res.json({file : req.file});
});
The server just gets stuck on loading.
So, my question is, does using gridfs, change how we normally use mongoose models with MongoDB?
My Post model (post.js)
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
//Post Schema
let PostSchema = new Schema({
caption : {
type : String,
required : true
},
image : {
type: String,
required: true
}
});
let Post = module.exports = mongoose.model('Post', PostSchema);
I want to use this Post collection, to store the image's filename that was uploaded, along with caption from client. Am doing something wrong, and I need help.
Another problem, right now am using a form to make this POST request. But when I try to use axios to make a post request to my API port(i.e 5000) from my client port(i.e 8080), I get CORS error.
How do I overcome this?
node.js reactjs mern
node.js reactjs mern
edited Jan 1 at 14:32
E_net4
12.2k63568
12.2k63568
asked Dec 31 '18 at 16:42
SorainOneSorainOne
34
34
add a comment |
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989594%2fhow-to-upload-image-to-mongodb-and-string-data-simultaneously-using-gridfs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53989594%2fhow-to-upload-image-to-mongodb-and-string-data-simultaneously-using-gridfs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown