Meteor-files: Images.insert problems on serverside with validations methods

Created on 1 Mar 2017  路  13Comments  路  Source: veliovgroup/Meteor-Files

```
console.log(file);
var uploadInstance = insertImage.call({
file: file,
type: template.data.type,
}, (err, res) => {
if (err) {
alert(err);
} else {
}
});


export const insertImage = new ValidatedMethod({
name: "insertImage",
validate: new SimpleSchema({
file: {
type: Object,
blackbox: true
},
type: String,

}).validator(),
run({ file, type }) {
console.log(file)
if (!Meteor.userId()) {
throw new Meteor.Error('insertImage.unauthorized',
'Cannot insert datas to Images');
}
Images.insert({
file: file,
streams: 'dynamic',
chunkSize: 'dynamic',
meta:{
type: type
}
},false);
}
});
```

I tried to use file as Object but it doesnt work, getting the Error "'"fileName" must me specified for base64 upload!'"
https://github.com/VeliovGroup/Meteor-Files/blob/master/files.coffee#L1633

any Ideas? :/ On clientside its working fine, but it has to work on serverside :x

.insert() (upload) question

All 13 comments

Hi @AdrianAbba ,

  1. .insert() is Client only method, see .write() and .load() methods
  2. How define schema and validations, read - here

Let me know if it helps

https://github.com/VeliovGroup/Meteor-Files/issues/337#issuecomment-277122688

Doing the same check like u did in this issueComment now =)
its weird to working with .write() on serverside, getting many errors.

but thx for the fast answer :D

its weird to working with .write() on serverside, getting many errors.

What kind of errors?

export const insertImage = new ValidatedMethod({
  name: "insertImage",
  validate: new SimpleSchema({
    fileName: String,
    type: String,
    metaType: String

  }).validator(),
  run({ fileName, type, metaType }) {
    if (!Meteor.userId()) {
      throw new Meteor.Error('insertImage.unauthorized',
        'Cannot insert datas to Images');
    }
    Images.write(new Buffer(fileName), {
      fileName: fileName,
      type: type,
      meta:{
        type:metaType,
      }
    }, function (error, fileRef) {
      if (error) {
        throw error;
      }
    });  
  }
});

i tried first smt like this.
and got the massage Images.write is not a function

Check what is in console.log(Images) before Images.write()

FilesCollection
_checkAccess:(http)
_methodNames:Object
_supportWebWorker:true
_webWorkerUrl:"blob:http://localhost:3000/4a935d14-f22f-473d-a121-32fcd63e3247"
allowClientCode:true
chunkSize:524288
collection:ns.Collection
collectionName:"Images"
ddp:Object
debug:false
downloadRoute:"/cdn/storage"
namingFunction:false
onBeforeUpload:(file)
onInitiateUpload:false
onbeforeunloadMessage:"Upload in a progress... Do you want to abort?"
protected:false
public:false
schema:Object
storagePath:()
__proto__:EventEmitter

Well, it's Client not a Server.
In short - use .insert() on Client and .write() on Server. And use Meteor.isServer to check if it Server or Client

indeed, its client Oo but it should be on server :o
ill write back :D

image

the browser console says to "console.log(Meteor.isServer);" : false
but in cmd it says : true

now im confused :x

  • cmd (a.k.a. Terminal) - is Server
  • Browser console - is Client

Hi @AdrianAbba any news on your end?

had not much time :/ so i used "Meteor.isServer" check on the onBeforeUpload function =)
its working fine :)

I'll assume it as solved.
Feel free to reopen it in case if issue still persists on your end.

Please, support this project by:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mking200 picture mking200  路  4Comments

RE-N-Y picture RE-N-Y  路  3Comments

msgfxeg picture msgfxeg  路  3Comments

ck23onGithub picture ck23onGithub  路  3Comments

JanSchuermannPH picture JanSchuermannPH  路  3Comments