Meteor method gets data as undefined

131 Views Asked by At

So I have this method in my component

uploadCallback (file) {
    // TODO: Integrate dropbox with its SDK
    // TODO: Pass the link to the editor
    return new Promise(
      (resolve, reject) => {
        console.log('uploadCallback promise')
        console.log('file', file)
        const dataObject = {
          file,
          resolve,
          reject
        }
        console.log('dataObject', dataObject)
        Meteor.call('uploadToDropbox', dataObject, function (error, result) {
          console.log('uploadToDropbox callback')
          if (error) {
            console.log('error', error)
          }
          if (result) {
            console.log('result', result)
          }
        })
      }
    )
  }

In my dataObject I am getting everything as needed. Here is what the console logs

uploadCallback promise
file File {name: "nodejs-2560x1440.png", lastModified: 1485410804857, lastModifiedDate: Thu Jan 26 2017 10:06:44 GMT+0400 (+04), webkitRelativePath: "", size: 1699460…}
dataObject Object {file: File}
uploadToDropbox callback

So everything seems to be ok here.

And here is my server code

import { Meteor } from 'meteor/meteor'
import Dropbox from 'dropbox'

console.log('dropbox settings', Meteor.settings.dropbox)
const dbx = new Dropbox({accessToken: Meteor.settings.dropbox.accessToken})

Meteor.methods({
  'uploadToDropbox': function (dataObject) {
    console.log('dataObject', dataObject)
    const { file } = dataObject
    console.log('file', file)
    const { resolve, reject } = dataObject
    console.log('resolve', resolve)
    console.log('reject', reject)
    dbx.filesUpload({path: '/' + file.name, contents: file})
      .then(function (response) {
        console.log(response)
        resolve({ data: { link: 'http://dummy_image_src.com' } })
      })
      .catch(function (error) {
        console.error(error)
        reject('some error')
      })
    return false
  }
})

The problem is here. dataObject is being passed almost empty This is what the server logs

I20170217-11:44:36.141(4)? dataObject { file: {} }
I20170217-11:44:36.143(4)? file {}
I20170217-11:44:36.143(4)? resolve undefined
I20170217-11:44:36.144(4)? reject undefined
W20170217-11:44:36.371(4)? (STDERR) [TypeError: first argument must be a string or Buffer]

So why is this happening?

2

There are 2 best solutions below

0
On BEST ANSWER

i suspect that File you're trying to pass to the method is a file handle. if true, then that's not going to work: even if the server did get that info, it has no access to your local filesystem to grab those bytes.

your solution is going to take 1 of 2 forms:

client uploads to dropbox

  1. client reads bytes from file system into memory
  2. client uploads bytes to dropbox
  3. client receives some dropbox metadata about the uploaded file (e.g. location)
  4. client calls server with that metadata info
  5. server saves that info to db

server uploads to dropbox

  1. client reads bytes from file system into memory
  2. client formats that data into something that can be handled by JSON
  3. client calls server with that JSON object
  4. server uploads bytes to dropbox
  5. server receives some dropbox metadata about the uploaded file (e.g. location)
  6. server saves that info to db

which to do? it depends on which dropbox package/solution you're using and how you want to structure your app.

0
On

You are returning a promises not data, you have to wait for result and then return data.