socketio-file-upload icon indicating copy to clipboard operation
socketio-file-upload copied to clipboard

Server to Client Meta Data doesn't work with await function

Open jacobprouse opened this issue 6 years ago • 1 comments

Hi, I'm trying to set server-client meta data in the listener uploader.on('saved') as in the npm docs. But accessing the meta data on the client side has event.detail.data returning undefined on the client side

Server Code

uploader.on('saved', async function (e) {
    console.log('Uploader: Saved')
    try {
      let result = await bar(e.file)
      if (res) {
        e.file.clientDetail.data = { 'foo': result }
      }
    } catch (error) {
      console.log(error)
    }
})

Client Code

this.uploader.addEventListener('complete', function (event) {
    console.log(event)
    console.log('the user has completed file: ', event.detail.data)
})

I tried packing the function into a object declaration on the server side as below

uploader.on('saved', async function (e) {
    console.log('Uploader: Saved')
    try {
      e.file.clientDetail.data = { 'foo':  await bar(e.file) }
    } catch (error) {
      log(error)
    }
})

But it returned as undefined on client still. I tried not awaiting the function in the listener

.
e.file.clientDetail.data = { 'foo':  function(e.file) }
.

and it returned {foo: {}} on the client side. So I'm wondering if / how you can do async operations inside the listener and be able to return it in meta data. Thanks!

jacobprouse avatar May 16 '19 16:05 jacobprouse

find in the node_modules the socketio-file-upload and change this line in server.js :

socket.emit(_getTopicName("_complete"), _wrapData({
			id: id,
			success: success,
			detail : fileInfo.clientDetail
}, "complete"));

TO:

socket.emit(_getTopicName("_complete"), _wrapData({
			id: id,
			success: success,
			detail: typeof fileInfo.clientDetail.then === 'function' ? await fileInfo.clientDetail:fileInfo.clientDetail
}, "complete"));

xeroxstar avatar May 31 '19 16:05 xeroxstar