簡體   English   中英

將客戶端變量發送到流星中的服務器

[英]Send Client side variable to Server in Meteor

似乎有這樣的事情,但恰恰相反 這是最接近的,但我沒有存儲任何東西。

我僅在其他帖子/答案中使用stripe收費:

客戶:

我正在使用jQuery從一個輸入字段中獲取值:

<input type="text" id="amount" name="amount"/>
var amount = $("#amount").val() *100; // the js client

服務器:

要收取卡費用,您將遇到以下情況:

amount: 7000, // I want to remove the static value

我需要這樣的東西:

amount: amount

我仍在學習js,我以為jquery可以用,但我想不是。 我嘗試過將amount傳遞給isServer函數,但這不起作用。 有其他選擇嗎?

我也嘗試過使用var amount = document.getElementById('#amount'); 但是document沒有定義。

編輯:

if (Meteor.isServer) {
  Meteor.methods({
    'chargeCard': function(stripeToken, amount) {
      check(stripeToken, Object);
      var stripe = Meteor.npmRequire('stripe')('sk_test_rand');

      stripe.customers.create({
        email: 'a@aol.com'
      }).then(function() {
        return stripe.charges.create({
          amount: amount,
          currency: 'gbp',
          source: stripeToken.id
        });
      }).then(function(err, charge) {
        // New charge created on a new customer
        console.log(err, charge);
      }, function(err) {
        // Deal with an error
        console.log('Card not charge')
      });
    }
  });
}

要從客戶端在服務器上調用流星方法,您需要使用meteor.call

Meteor.call('chargeCard', token, amount, function (err, res) {
  if (err) {
    console.log(err);
    return;
  }
  // success
  // if you return anything from the server you can find it with res
  console.log(res);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM