2015-08-28 57 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);   
});