Sunday, 14 May 2017

mongoose-gridfs extra fields

I am saving files using mongoose-gridfs to gridfs, but I would like to add extra fields (basically the id of where it belongs to), but the method below doesn't work.

var gridfsotherfiles = require('mongoose-gridfs')({
    collection:'videos',
    model:'otherfiles'
});

    gridfsotherfiles.write({
        filename:thisfile.originalname, 
        contentType:thisfile.mimetype,
        measurement:ObjectId(req.body.id),
        }, 
        stream, 
    function(error, createdFile){
    console.log(createdFile._id)       
});

I tried using mongoose-schema-extend

var thisthing = require('mongoose-gridfs')({
  collection:'otherfiles',
  model:'otherfiles_original',
});
var otherfilesSchema = thisthing.schema.extend({measurement:{type:Schema.Types.ObjectId, default:null})

var gridfsotherfiles = mongoose.model("otherfiles",otherfilesSchema)

but when trying to save the file I get this error an error (node:1150) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MissingSchemaError: Schema hasn't been registered for model "otherfiles_original". Use mongoose.model(name, schema)

but it shouldn't even by tring to use otherfiles_original now. Any ideas how I could solve this?



via fbence

No comments:

Post a Comment