Javascript &引用;出了点问题。”;函数getfloat出错

Javascript &引用;出了点问题。”;函数getfloat出错,javascript,Javascript,这段代码在最后一部分旁边运行得很好,我看不出哪里可能会出错。我对它进行了测试,主要问题是Trade.prototype.getFloat可能是回调idk(Trade.prototype.getFloat是我从其他用户在堆栈上的3y帖子中得到的) 3y问题:除了这行代码:“inventory[asset.assetid].floatvalue=getFloat”之外,这段代码工作得非常好。正如您所看到的,它位于异步模式下,此行初始化一个请求以获取某些值,但它无法获取该值,因为该值未定义。我对它进行

这段代码在最后一部分旁边运行得很好,我看不出哪里可能会出错。我对它进行了测试,主要问题是Trade.prototype.getFloat可能是回调idk(Trade.prototype.getFloat是我从其他用户在堆栈上的3y帖子中得到的)

3y问题:除了这行代码:“inventory[asset.assetid].floatvalue=getFloat”之外,这段代码工作得非常好。正如您所看到的,它位于异步模式下,此行初始化一个请求以获取某些值,但它无法获取该值,因为该值未定义。我对它进行了测试,发现请求中的主要问题也是异步的。因此,答案是如何停止异步模式并等待请求返回。

'use strict'

const config = require('../config')
const request = require('request')
const async = require('async')
const Trade = require('./index')

const MAX_RETRIES = 3
const API_URL = 'https://api.steamapis.com/steam/inventory'

Trade.prototype.getInventory = function getInventory(steamID64, appID, contextID, callback, retries) {
    request(`${API_URL}/${steamID64}/${appID}/${contextID}?api_key=${config.SteamApisKey}`, (error, response, body) => {
        if (!error && response.statusCode === 200) {
            const items = JSON.parse(body)
            const assets = items.assets
            const descriptions = items.descriptions

            const inventory = {}

            if (descriptions && assets) {
                async.forEach(descriptions, (description, cbDesc) => async.forEach(assets, (asset, cbAsset) => {
                    if (
                        description.classid !== asset.classid ||
                        !description.tradable || 
                        !description.marketable ||
                        description.market_hash_name.indexOf('Souvenir') > -1
                    ) {
                    return cbAsset()
                    }

                    if (typeof inventory[asset.assetid] !== 'undefined') {
                    return cbAsset()
                    }

                    const type = Trade.prototype.getItemType(description.market_hash_name, description.type)
                    const wear = Trade.prototype.getItemWear(description.market_hash_name)
                    const inspect = Trade.prototype.getInspect(steamID64, asset.assetid, description.actions)
                    const getFloat = Trade.prototype.getFloat(inspect, asset.assetid, function(_float){
                    var data = String(_float);
                    inventory[asset.assetid].floatvalue = data;
                    inventory[asset.assetid] = asset
                    inventory[asset.assetid].item_type = type
                    inventory[asset.assetid].item_wear = wear
                    inventory[asset.assetid].inspect = inspect
                    inventory[asset.assetid].data = {
                        background: description.background_color,
                        image: description.icon_url,
                        tradable: description.tradable,
                        marketable: description.marketable,
                        market_hash_name: description.market_hash_name,
                        type: description.type,
                        color: description.name_color,
                    };
                    return cbAsset();
                })
                }));
            }
            return callback(null, inventory)
        }
        let retry = retries
        if (typeof retries === 'undefined') {
            retry = 0
        }
        retry += 1
        if (retry <= MAX_RETRIES) {
            return Trade.prototype.getInventory(steamID64, appID, contextID, callback, retry)
        }
        let statusCode = null
        if (typeof response !== 'undefined' && typeof response.statusCode !== 'undefined') {
            statusCode = response.statusCode
        }
        return callback({ error, statusCode })
    })
}

Trade.prototype.getInventories = function getInventories(params, callback) {
    const inventories = {}
    async.each(params, (user, cb) => {
        Trade.prototype.getInventory(user.steamID64, user.appID, user.contextID, (err, data) => {
            inventories[user.id] = {}
            inventories[user.id] = {
                error: err,
                items: (!err) ? Object.keys(data).map(key => data[key]) : null,
            }
            cb()
        })
    }, () => {
        callback(inventories)
    })
}

Trade.prototype.getItemType = function getItemType(marketHashName, type) {
    if (marketHashName.indexOf('Key') !== -1) {
        return { value: 0, name: 'key' }
    }
    if (marketHashName.indexOf('★') !== -1) {
        return { value: 1, name: 'knife' }
    }
    if (
        type.indexOf('Classified') !== -1 ||
        type.indexOf('Contraband') !== -1 ||
        type.indexOf('Covert') !== -1
    ) {
        return { value: 2, name: 'rare_skin' }
    }
    if (
        type.indexOf('Consumer Grade') !== -1 ||
        type.indexOf('Base Grade') !== -1 ||
        type.indexOf('Graffiti') !== -1 ||
        type.indexOf('Sticker') !== -1 ||
        type.indexOf('Industrial Grade') !== -1
    ) {
        return { value: 4, name: 'misc' }
    }
    return { value: 3, name: 'weapon' }
}

Trade.prototype.getItemWear = function getItemWear(marketHashName) {
    if (marketHashName.indexOf('Factory New') !== -1) {
        return 'FN'
    }
    if (marketHashName.indexOf('Minimal Wear') !== -1) {
        return 'MW'
    }
    if (marketHashName.indexOf('Field-Tested') !== -1) {
        return 'FT'
    }
    if (marketHashName.indexOf('Well-Worn') !== -1) {
        return 'WW'
    }
    if (marketHashName.indexOf('Battle-Scarred') !== -1) {
        return 'BS'
    }
    return false
}
Trade.prototype.getInspect = function getInspect (steamID64, assetid, actions) {
    let inspectLink = null;                                           
    if (actions) {
        for (const a in actions) {
            if (actions[a].name.indexOf('Inspect') !== -1) {
                   inspectLink = actions[a].link
                   inspectLink = inspectLink.replace('%owner_steamid%', steamID64)
                   inspectLink = inspectLink.replace('%assetid%', assetid)
            }
        }
    }
    return inspectLink
}

Trade.prototype.getFloat = function getFloat (adding, callback) {
    request ("https://api.csgofloat.com:1738/?url=" + adding, (error, response, body) => {

     if (!error && response.statusCode == 200) {
         var floatBody = JSON.parse(body);
         var float = floatBody["iteminfo"]["floatvalue"];
         var id = id;
         if (float != "") {
             callback(float);
         } else {
             return "wrong";
         }
     } else {
        console.log('something goes wrong');
        return "wrong";
    }
    });
}

“严格使用”
const config=require(“../config”)
const request=require('请求')
const async=require('async')
const Trade=require('./索引')
常量最大重试次数=3
const API_URL='1〕https://api.steamapis.com/steam/inventory'
Trade.prototype.getInventory=函数getInventory(steamID64,appID,contextID,回调,重试){
请求(`${API_URL}/${streamid64}/${appID}/${contextID}?API_key=${config.streamapiskey}`,(错误、响应、正文)=>{
如果(!error&&response.statusCode==200){
const items=JSON.parse(正文)
const assets=items.assets
const descriptions=items.descriptions
const inventory={}
if(说明和资产){
async.forEach(说明,(说明,cbDesc)=>async.forEach(资产,(资产,cbAsset)=>{
如果(
description.classid!==asset.classid||
!description.tradable | |
!description.marketable||
说明.市场名称.索引(纪念品)>-1
) {
返回cbAsset()
}
if(库存类型[资产.资产ID]!==“未定义”){
返回cbAsset()
}
const type=Trade.prototype.getItemType(description.market\u hash\u name,description.type)
const wear=Trade.prototype.getItemWear(description.market\u hash\u name)
const inspect=Trade.prototype.getInspect(streamid64,asset.assetid,description.actions)
const getFloat=Trade.prototype.getFloat(inspect,asset.assetid,function(_float){
var数据=字符串(_float);
存货[asset.assetid].floatvalue=数据;
存货[asset.assetid]=资产
存货[asset.assetid]。物料类型=类型
存货[asset.assetid]。物料磨损=磨损
存货[asset.assetid].inspect=inspect
存货[asset.assetid]。数据={
背景:description.background\u颜色,
图片:description.icon\u url,
可交易的,可交易的,
可销售的,可销售的,
市场名称:description.market\u hash\u name,
type:description.type,
颜色:description.name\u颜色,
};
返回cbAsset();
})
}));
}
返回回调(空,库存)
}
让重试=重试
如果(重试的类型===“未定义”){
重试=0
}
重试+=1
如果(重试){
Trade.prototype.getInventory(user.streamid64,user.appID,user.contextID,(err,data)=>{
清单[user.id]={}
库存[用户id]={
错误:呃,
项:(!err)?Object.keys(data).map(key=>data[key]):null,
}
cb()
})
}, () => {
回收(库存)
})
}
Trade.prototype.getItemType=函数getItemType(marketHashName,类型){
if(marketHashName.indexOf('Key')!=-1){
返回{值:0,名称:'key'}
}
if(marketHashName.indexOf('★') !== -1) {
返回{值:1,名称:'刀'}
}
如果(
type.indexOf('Classified')!=-1||
type.indexOf(“违禁品”)!=-1||
type.indexOf('Covert')!=-1
) {
返回{value:2,名称:'ravel_skin'}
}
如果(
type.indexOf('Consumer Grade')!=-1||
type.indexOf('Base Grade')!=-1||
type.indexOf('Graffiti')!=-1||
type.indexOf('Sticker')!=-1||
类型indexOf(‘工业级’)!=-1
) {
返回{value:4,名称:'misc'}
}
返回{值:3,名称:'武器'}
}
Trade.prototype.getItemWear=函数getItemWear(marketHashName){
if(marketHashName.indexOf('Factory New')!=-1){
返回'FN'
}
if(marketHashName.indexOf(‘最小磨损’)!=-1){
返回“MW”
}
if(marketHashName.indexOf('Field-Tested')!=-1){
返回“FT”
}
if(marketHashName.indexOf('Well-wear')!=-1){
返回“WW”
}
if(marketHashName.indexOf('Battle-scared')!=-1){
返回“BS”
}
返回错误
}
Trade.prototype.getInspect=函数getInspect(steamID64,assetid,actions){
设inspectLink=null;
如果(行动){
for(活动中的常数a){
如果(操作[a].name.indexOf('Inspect')!=-1){
检查链接=操作[a]。链接
inspectLink=inspectLink.replace(“%owner\u steamid%”,steamID64)
inspectLink=inspectLink.replace(“%assetid%”,assetid)
}
}
}
返回检查链接
}
Trade.prototype.getFloat=函数getFloat(添加、回调){
请求(“https://api.csgofloat.com:1738/?url=“+添加,(错误、响应、正文)=>{