简体   繁体   中英

Meteor.js: Wait for server to finish

I'm running into a scenario where my meteor server call is posting to a remote URL and then returning the result. However, my meteor client is expecting a result right away and its receiving an empty string (the default return).

What is the correct way of implementing this?

Meteor.methods({
run: function(options){
            return HTTP.post(apiUrl, {
                    params:
                    {
                        "headers": headers
                    }
                },
                function (error, result)
                {
                    if (error)
                    {
                        console.log("error: " + error);
                    }
                    else
                    {
                        console.log("result: " + JSON.stringify(result));
                        console.log(result.content);
                    }
                })
});

on my client

Meteor.call('run', '1', function(err,response) {
        if(err) {
            console.log(err);        
            return;
        }else{
            r = response;
            console.log(JSON.stringify(r));
            FileSystem.update({ _id: fileid }, { $set: {taskid:taskid} }, function (e, t) {
                if (e) {

                }else{

                }
            });
        }
    });

I'm expecting on the client side that it waits for the full result to come in which contains the desired data to save to data base (taskid).

You are calling HTTP.post asynchronously. Just remove the callback function and it becomes synchronous, ie, you will get a return value that contains the result of the call:

Meteor.methods({
    run: function(options){
        return HTTP.post(apiUrl, {
            params:
            {
                "headers": headers
            }
        });
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM