Skip to content

Add support for coercion by endpoints with type File #318

@devInterests

Description

@devInterests

If you have a remote method with a parameter that's type: 'file' you receive following error:
"Error: Invalid value for argument 'file' of type 'file': [object Object]. Received type was converted to object.

As far as I understood the error is in function convertToBasicRemotingType in shared-method.js:

function convertToBasicRemotingType(type) {
  if (Array.isArray(type)) {
    return type.map(convertToBasicRemotingType);
  }
  if (typeof type === 'object') {
    type = type.modelName || type.name;
  }

  type = String(type).toLowerCase();

  switch (type) {
    case 'string':
    case 'number':
    case 'date':
    case 'boolean':
    case 'buffer':
    case 'object':
    case 'file':
    case 'any':
      return type;
    case 'array':
      return ['any'].map(convertToBasicRemotingType);
    default:
      // custom types like MyModel
      return 'object';
  }
}

The type file is supported as a separate type. The problem is that the arg is coming as type object.
My fix at the moment is to use object as arg type or to use a older version where the file type is not supported and becomes default type object.
But i would like to use the correct type: file.

My interface definition:

model.remoteMethod( 
       'fileUpload',
        {
            http:{ verb:'Post', source: 'body', path: '/upload/:id'},
            description: '',
            accepts: [
                {arg: 'id', type: 'string', http: { source: 'path' }, required: true},
                {arg: 'file', type: 'file', http: { source: 'body' }, required: true},
                {arg: 'req', type: 'object', http: {source: 'req'}}
            ],
            returns: {arg: 'res', type: 'object', root:true}
        });

Perhaps I am doing something wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions