【发布时间】:2018-03-16 11:33:22
【问题描述】:
)
我正在开发一个 node.js express 自定义 API 项目。 目前我在尝试推送数据时遇到问题......
这是我的代码:
module.exports = function(config, steamClient, csgo, database, teamspeakClient, router) {
var async = require('async'),
apicache = require('apicache'),
cache = apicache.middleware,
mysql = require('mysql');
async.waterfall([
function(callback) {
router.get('/CsgoUser/requestPlayersProfile/v1/:steamids', function(req, res) {
var steamids = req.params.steamids.split(',');
var csgo_data = [];
steamClient.getPersonas(steamids, function(resp) {
Object.keys(resp).forEach(function (steamid){
if (!steamClient.myFriends.hasOwnProperty(steamid)) {
if (resp[steamid].game_played_app_id == 730) {
csgo.requestPlayersProfile(steamid, function(result) {
console.log(result);
csgo_data.push(csgo_data, {steamid: steamid, response: result});
});
}else{
csgo_data.push({steamid: steamid, response: steamid + " is not playing csgo"});
}
}else{
csgo_data.push({steamid: steamid, response: steamid + " Is not bot´s friend..."});
}
});
res.json({ success: true, code: 200, response: csgo_data });
});
});
}
])};
结果如下:
{"success":true,"code":200,"response":[{"steamid":"76561198013250658","response":"76561198013250658 不是机器人的朋友..."},{" steamid":"76561198263874163","response":"76561198263874163 不是 bot 的朋友..."},{"steamid":"76561198156967342","response":"76561198156967342 不是 bot 的朋友..." }]}
调用的时候问题来了
csgo.requestPlayersProfile(steamid, function(result) {
console.log(result);
csgo_data.push(csgo_data, {steamid: steamid, response: result});});
console.log 的输出数据(例如:{"ID":"hello"})在终端上正确显示,但未推送到 csgo_data 数组...
请尝试给我一个解释:-)
【问题讨论】:
-
您遇到错误了吗?
TypeError: second argument to Function.prototype.apply must be an array... 为什么你在做csgo_data.push.apply(csgo_data, {steamid: steamid, response: result});... 而不是csgo_data.push({steamid: steamid, response: result}); -
@JaromandaX 不,我没有收到任何错误。
-
只是为了测试xd..我也试过csgo_data.push方法。
-
@JaromandaX 知道有什么问题吗?
-
如果
csgo.requestPlayersProfile是异步的,回调中的push()命令将在res.json()之后被调用
标签: javascript arrays node.js api express