简体   繁体   English

NodeJS和导出“没有方法..”

[英]NodeJS and exports “has no method ..”

Im writing an api for project and got a rare error with this function 我正在为项目编写一个api,此功能出现罕见错误

exports.getSummonerId = function(sum, callback) {

   var summoners = {};
   var summoner = sum.replace(/\s+/g, '');

   request("https://na.api.pvp.net/api/lol/"+region+"/v1.4/summoner/by-name/"+summoner+"?api_key="+api_key, function(error, response, body) {
         summoners[summoner] = JSON.parse(body);
         callback(summoners[summoner][summoner].id);
   });
}

The call is: 电话是:

var lol = require('./apiwrapper.js');


lol.getSummonerId(function(data) {
   console.log(data);
});

I get: 我得到:

lol-test/apiwrapper.js:11
   var summoner = sum.replace(/\s+/g, '');
                      ^
TypeError: Object function (data) {
   console.log(data);
} has no method 'replace'

If I run the code without the exports, directly from apiwrapper.js everything runs normal. 如果我运行的代码没有导出,则直接从apiwrapper.js运行一切正常。

Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

Your function has 2 parameters, summoner name and callback function. 您的函数有2个参数,召唤者名称和回调函数。 Try this: 尝试这个:

lol.getSummonerId("player", function(data) {
   console.log(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM