我怎样才能在我的客户端获得流星异步方法的结果

How can i get result of a meteor async method in my client

我必须在服务器上执行 python 脚本。这个脚本需要时间来回答,但我需要我的脚本在我的客户端中的响应。

/* here is the server side */

async function myRequest(options) {
    await PythonShell.run('converter.py', options, function (err, result) {
        if (err) throw err;
        console.log("in my request")
        console.log(result);
        return result;
    });
}

Meteor.methods({
    'findRealName': async function (id) {
        let options = {
            mode: 'text',
            pythonPath: '/usr/bin/python',
            pythonOptions: ['-u'], // get print results in real-time
            scriptPath: '/Users/eliott/Desktop/influFinder/client/',
            args: ['-i', id]
        };
        var result = await myRequest(options)
        console.log("in find name")
        console.log(result)
        return result
    }
});

/* here is the client side */

Template.search.events({
    'click #searchButton': function() {
        var id = 2220626204
        var result = Meteor.call('findRealName', [id], (error, res) => {
            console.log("in client")
            console.log(res)
        })
        console.log("in client 2")
        console.log(result)
    }
});

服务器端输出: 在查找名称 不明确的 在我的要求下 用户名

客户端输出: 在客户端 2 不明确的 在客户端 未定义

在服务器中,我打印了良好的结果,但在客户端中,无论我做什么,它总是 "undefined"。 我只想获得可以在服务器端控制台日志中打印并将其存储在客户端变量中的结果

你可以尝试使用 Promises,像这样:

async function myRequest(options) {
    return new Promise(resolve => {
        PythonShell.run('converter.py', options, function (err, result) {
            if (err) throw err;
            console.log("in my request")
            console.log(result);
            resolve(result);
        });
    });
}

现在无法测试,但应该可以。