相关疑难解决方法(0)

如何从异步调用返回响应?

我有一个foo发出Ajax请求的函数.我怎样才能从中回复foo

我尝试从success回调中返回值,并将响应分配给函数内部的局部变量并返回该变量,但这些方法都没有实际返回响应.

function foo() {
    var result;

    $.ajax({
        url: '...',
        success: function(response) {
            result = response;
            // return response; // <- I tried that one as well
        }
    });

    return result;
}

var result = foo(); // It always ends up being `undefined`.
Run Code Online (Sandbox Code Playgroud)

javascript ajax jquery asynchronous xmlhttprequest

5208
推荐指数
38
解决办法
134万
查看次数

如何从异步回调函数返回值?

在SO中多次询问这个问题.但我还是得不到东西.

我想从回调中获得一些价值.请查看下面的脚本以获得说明.

function foo(address){

      // google map stuff
      geocoder.geocode( { 'address': address}, function(results, status) {
          results[0].geometry.location; // I want to return this value
      })

    }
    foo(); //result should be results[0].geometry.location; value
Run Code Online (Sandbox Code Playgroud)

如果我试图返回该值只是"未定义".我从SO那里听了一些想法,但仍然失败了.

那些是:

function foo(address){
    var returnvalue;    
    geocoder.geocode( { 'address': address}, function(results, status) {
        returnvalue = results[0].geometry.location; 
    })
    return returnvalue; 
}
foo(); //still undefined
Run Code Online (Sandbox Code Playgroud)

javascript asynchronous callback javascript-objects

440
推荐指数
3
解决办法
33万
查看次数

Meteor:在服务器上正确使用Meteor.wrapAsync

背景

我正在尝试将条带付款整合到我的网站中.我需要使用私有条带密钥创建条带用户.我将此密钥存储在我的服务器上,并调用服务器方法来创建用户.也许有另一种方法可以实现这一目标?这是条纹api(为方便起见,下面复制):https: //stripe.com/docs/api/node#create_customer

//stripe api call
var Stripe = StripeAPI('my_secret_key');

Stripe.customers.create({
  description: 'Customer for test@example.com',
  card: "foobar" // obtained with Stripe.js
}, function(err, customer) {
  // asynchronously called
});
Run Code Online (Sandbox Code Playgroud)

我的尝试和结果

我一直在使用不同服务器代码的相同客户端代码.所有尝试都会在客户端的console.log(...)上立即给出undefined,但在服务器console.log(...)上给出正确的响应:

//client
Meteor.call('stripeCreateUser', options, function(err, result) {
  console.log(err, result);
});

//server attempt 1
var Stripe = StripeAPI('my_secret_key');

Meteor.methods({
    stripeCreateUser: function(options) {  
        return Meteor.wrapAsync(Stripe.customers.create({
            description: 'Woot! A new customer!',
            card: options.ccToken,
            plan: options.pricingPlan
        }, function (err, res) {
            console.log(res, err);
            return (res || err);
        }));
    }
});

//server attempt …
Run Code Online (Sandbox Code Playgroud)

javascript stripe-payments meteor

50
推荐指数
3
解决办法
3万
查看次数

如何让Meteor.Call返回模板的值?

我试图理解这个关于这个概念的帖子,但是,我没有得到它.我有以下简单的设置:

/server/test.js
Meteor.methods({ 
  abc: function() {
    var result = {};
    result.foo = "Hello ";
    result.bar = "World!";
    return result;
  }
});

/client/myapp.js
var q = Meteor.call('abc');
console.log(q);
Run Code Online (Sandbox Code Playgroud)

此结构返回到控制台undefined.

如果我将myapp.js文件更改为:

Meteor.call('abc', function(err, data) {
  !err ? console.log(data) : console.log(err);
}
Run Code Online (Sandbox Code Playgroud)

我收到了Object我的控制台.

理想情况下,这是我希望能够做到的,但它不起作用,在控制台中说明: Cannot read property 'greeting' of undefined

/client/myapp.js
var q = Meteor.call('abc');

Template.hello.greeting = function() {
   return q.foo;
}
Run Code Online (Sandbox Code Playgroud)

将数据从服务器对象传递到模板的任何帮助将不胜感激.我还在学习JavaScript和Meteor.

谢谢!

callback meteor

41
推荐指数
1
解决办法
3万
查看次数

Meteor:在Meteor.method中调用异步函数并返回结果

我想在Meteor方法中调用异步函数,然后将该函数的结果返回给Meteor.call.

(怎么样)可能吗?

Meteor.methods({
  my_function: function(arg1, arg2) {
    //Call other asynchronous function and return result or throw error
  }
});
Run Code Online (Sandbox Code Playgroud)

javascript meteor

39
推荐指数
4
解决办法
3万
查看次数

Meteor.methods返回undefined

我正在使用流星0.6.4.

Meteor.methods({
  random: function(top){
    var random = Math.floor((Math.random()*(top+1)));
    return random;
  }
});
Run Code Online (Sandbox Code Playgroud)

每当我执行时它返回undefined

Meteor.call('random', 10);
Run Code Online (Sandbox Code Playgroud)

我有什么想法可以超越这个?

meteor

31
推荐指数
1
解决办法
2万
查看次数