veliovgroup / Meteor-Files

🚀 Upload files via DDP or HTTP to ☄️ Meteor server FS, AWS, GridFS, DropBox or Google Drive. Fast, secure and robust.
https://packosphere.com/ostrio/files
BSD 3-Clause "New" or "Revised" License
1.11k stars 166 forks source link

Match error: Unknown key in field #887

Closed EdoElgx closed 6 months ago

EdoElgx commented 6 months ago

I'm having an issue:

Client Code:

      const file = event.target.files[0]
      if (!file) {
        console.log('No file')
        return
      }
      const uploadInstance = AttachmentsCollection.insert({
        file,
        chunkSize: 'dynamic',
        abstract: 'I am just testing'
      }, false) // <== IT FAILS HERE
      console.log('Setting upload instance', uploadInstance) // <== IT'S NEVER SHOWN
      uploadInstance.on('start', () => {
        setUploadStatus(t('institutions.agreements.upload.started'))
      })
      // ...

Server Code:

import { Meteor } from 'meteor/meteor'
import { FilesCollection } from 'meteor/ostrio:files'

const attachmentSchema = {
  ...FilesCollection.schema,
  abstract: { type: String, optional: true }
}

const AttachmentsCollection = new FilesCollection({
  collectionName: 'attachments',
  allowClientCode: false,
  schema: attachmentSchema,
  storagePath: function () {
    if (process.env.NODE_ENV === 'development') {
      return '../attachments'
    } else {
      return process.env.ATTACHMENT_LOCATION
    }
  },
  debug: process.env.NODE_ENV === 'development'
})

AttachmentsCollection.collection.attachSchema(AttachmentsCollection.schema)
dr-dimitru commented 6 months ago

Hello @EdoElgx ,

Remove abstract: 'I am just testing' line from:

const uploadInstance = AttachmentsCollection.insert({
  file,
  chunkSize: 'dynamic',
}, false);

If you need to pass additional data along with a file use meta object, like:

const uploadInstance = AttachmentsCollection.insert({
  file,
  chunkSize: 'dynamic',
  meta: {
    abstract: 'I am just testing'
  }
}, false);

Hope that helps, let me know

EdoElgx commented 6 months ago

Thank you, @dr-dimitru. I'll try that.

Is there any way to validate what I pass in the meta property?

dr-dimitru commented 6 months ago

@EdoElgx yes you can define schema for meta object

EdoElgx commented 6 months ago

Thanks @dr-dimitru I'll try that.

Inserting without schema worked perfectly.

dr-dimitru commented 6 months ago

@EdoElgx I'm glad it was solved quickly