I am currently developing an API for my application, utilizing Mongoose, Express, and GridFS-Stream. My aim is to create a proper Schema for the articles that users will generate:
var articleSchema = mongoose.Schema({
title:String,
author:String,
type: String,
images: {type: Schema.Types.ObjectId, ref: "fs.files"},
datePublished: { type: Date, default: Date.now },
content: String
})
var Article = mongoose.model("article", articleSchema, "articles");
I also have set up Grid-FS for handling image uploads by users:
api.post('/file', fileUpload.single("image"), function(req, res) {
var path = req.file.path;
var gridWriteStream = gfs.createWriteStream(path)
.on('close',function(){
//remove file on close of mongo connection
setTimeout(function(){
fs.unlink(req.file.path);
},1000);
})
var readStream = fs.createReadStream(path)
.on('end',function(){
res.status(200).json({"id":readStream.id});
console.log(readStream);
})
.on('error',function(){
res.status(500).send("Something went wrong. :(");
})
.pipe(gridWriteStream)
});
The current setup automatically uploads user-chosen images using gridfs-stream, places them in a temporary folder, deletes them upon successful upload to the MongoDB server, and returns the ObjectId in the console. However, we need to link this ID with the articleSchema so that when the article is accessed in the app, the associated image is displayed.
Upon creation/update of an article when the user submits it:
createArticle(event) {
event.preventDefault();
var article = {
type: this.refs.type.getValue(),
author: this.refs.author.getValue(),
title: this.refs.title.getValue(),
content: this.refs.pm.getContent('html')
};
var image = {
images: this.refs.imageUpload.state.imageString
};
var id = {_id: this.refs.id.getValue()};
var payload = _.merge(id, article, image);
var newPayload = _.merge(article, image)
if(this.props.params.id){
superagent.put("http://"+this.context.config.API_SERVER+"/api/v1.0/article/").send(payload).end((err, res) => {
err ? console.log(err) : console.log(res);
});
} else {
superagent.post("http://"+this.context.config.API_SERVER+"/api/v1.0/article").send(newPayload).end((err, res) => {
err ? console.log(err) : console.log(res);
this.replaceState(this.getInitialState())
this.refs.articleForm.reset();
});
}
In summary, I need to retrieve the ObjectId of the uploaded image from fs.files or fs.chunks and associate it with the schema when the user submits an article. Despite attempting a readstream on submission, I'm struggling to access the ID or filename required for association.
The data is stored in the MongoDB database under fs.files and fs.chunks, but I'm finding it challenging to extract or link it without knowing the specific ObjectId. How can I obtain the objectID from fs.files or fs.chunks to connect it with the schema? And how do I reference these components within the schema for identification?
If further information is necessary or if clarification is required, please let me know as I tend to overlook details. Thank you.