2024-05-26

Meteor 3 - Client-side awaiting for server-side methods execution

// This works best!

const res = await Meteor.callAsync("addTestActivity", {});

res; //res is undefined


// This works too

const res = await Meteor.callAsync("addTestActivity", {})

    .then((error, response) => {

        if(error) return error;

        return response;

    }); 

res;



// This works! (though it unnecessaryly wraps a Meteor call synchronous method call in a promise, see above)

const res = await new Promise((resolve, reject) => {

    Meteor.call("addTestActivity", {}, 

        (error, response) => {

            if(error) reject(error);

            resolve(response);

            return response;

        }

    );

});

res; // res is good, the value returned by the method



//This does not work

const res = await Meteor.call("addTestActivity", {}, 

    (error, response) => {

        if(error) return error;

        return response;

    }

)); //res is undefined



// This does not work either (no callback)

const res = await Meteor.call("addTestActivity", {});

res; //res is undefined



// For quick reference, here is the server-side method

import {ValidatedMethod} from "meteor/mdg:validated-method";

import SimpleSchema from 'simpl-schema';

export const addTestActivity = new ValidatedMethod({

    name: "addTestActivity",

    validate: new SimpleSchema({}).validator(),

    async run({}) {

return await activity.insertAsync({x:1, y:2, z:3});

    }

});