2015-08-28 29 views
3

我正在使用Braintree nodejs Sdk作爲我的項目之一, 我有使用customer.find()函數的問題。 origina link is hereBraintree customer.find()不能使用Nodejs

當我以這種方式直接傳遞像'207'這樣的用戶標識時,它正在工作。

var braintree = require("braintree"); 
var gateway = require("./bt"); 

gateway.customer.find('207', function(err, customer) {    
       console.log("customer:",customer);   
}); 

但是當我試圖通過動態ID,它不工作;

btuserID = data.userId; 
gateway.customer.find(btuserID, function(err, customer) {    
       console.log("customer:",customer);   
      }); 

我認爲它的參數類型是字符串,所以我怎麼可以傳遞參數沒有雙引號?

完全錯誤:

/home/api/node_modules/loopback-connector-mysql/node_modules/mysql/lib/protocol/Parser.js:82 
     throw err; 
      ^
TypeError: undefined is not a function 
    at CustomerGateway.find (/home/api/node_modules/braintree/lib/braintree/customer_gateway.js:37:20) 
+0

你能嘗試做'的console.log(btuserID)'的'gateway.customer.find'前行,只是爲了確保'btuserID'是用戶ID你期望它是? –

+0

是的,我做到了這一點,它是207沒有報價。 –

+1

我在布倫特裏工作。客戶ID存儲爲字符串,因此您可以在將'btuserID'傳遞給'find'之前將其轉換爲字符串。根據你所得到的錯誤,然而,可能會有另一個問題。請聯繫[Braintree支持](https://support.braintreepayments.com)(可能會提供更多的服務器端代碼)以獲得進一步的幫助。 – cdeist

回答

5

如果您嘗試

console.log(typeof(btuserID)); 

我懷疑你會看到類型是Number,如果是的話請嘗試以下建議。

將您的任務更改爲此將數字轉換爲字符串。

btuserID = data.userId.toString(); 

如果您查看braintree Node.js庫的源代碼,您會看到它首先嚐試修剪該字符串。如果你在數字上這樣做,你會期望這會失敗。

https://github.com/braintree/braintree_node/blob/master/src/braintree/customer_gateway.coffee

+0

完美的解決方案 –

2

我今天碰上了同樣的問題。 你只需要做:

gateway.customer.find(JSON.stringify(btuserID), function(err,customer){ 
       console.log("customer:",customer);   
});