Skip to content

Can't call event in async mode #26

@ruiisidro

Description

@ruiisidro

I have this scenario:

  1. I've created a collection to log some data.
    When a user posts to this collection I want to call a custom dpd-event where I will call an external service asynchronously. When the external call ends I want to finish up some details in the post data.
//this is the custom dpd-event
//file resources\cloudinarysrv\post.js
var cloudinary = require('cloudinary');

var done = ctx.done          // remember this to end the response (async)
ctx.done = function () { }      // dummy function for autoresponse (sync)

cloudinary.config({
  cloud_name: 'my_cloud_name',
  api_key: 'my_key',
  api_secret: 'my_secret'
});

cloudinary.v2.uploader.upload(ctx.body.ficheiro, function (error, result) {
  if (error) {
    cancel(error.message, 401);
  }
  else {
    console.log("resposta do  cloudinary");
    console.log(JSON.stringify(result));
    setResult(result);
  }
});

As to the collection:

//this is the collection
//resources\exchange\validate.js
var self = this;

if (self.logotipo) {
    dpd.cloudinarysrv.post({ ficheiro: self.logotipo }, function (result, error) {
        if (error) {
            self.logotipo = "upload error image default...";
            self.secureLogotipo = "upload error secured image default...";
        }
        else {
            console.log(JSON.stringify(result));
            console.log("já guardou na cloud e está de volta...");
//this should be get after the external call in dpd-event finishes but the call is not being done asynchronously
            self.logotipo = result.url;
            self.secureLogotipo = result.secure_url;
        }
    });
}
else {
    self.logotipo = "imagem default...";
    self.secureLogotipo = "imagem default segura...";
}

I'm not grasping the async calls concept... can someone help?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions