Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jacobprouse opened this issue May 16, 2019 · 1 comment
Open

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

jacobprouse opened this issue May 16, 2019 · 1 comment

Comments

@jacobprouse
Copy link

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!

@xeroxstar
Copy link

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"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants