Jump to content
McKay Development

Recommended Posts

Posted

Good day,

So I am kind of new to js and this may be a noob question, if it is I am very sorry for asking. But here is what I am having issues with:

const getInventory = () => {
    manager.loadInventory(7302true, (err, inventory) => {
        if (inventory) {
            return inventory;
        }
        if (err) {
            console.log(err);
        }
    })
}

So I export this function, but when I try console logging it, it doesn't return anything, just undefined. But when I put inventory into a callback, I get it. Is there something I am missing here? 

Thanks!

Posted (edited)

I also made this mistake as a beginner. 

You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory.

You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched.

const getInventory = (callback) => {
    manager.loadInventory(730, 2, true, (err, inventory) => {
        if (err) {
            callback(err);
        }

        else {
            callback(null, inventory);
        }
    })
}

I personally prefer to check for errors first, but whatever you prefer you can change it to. 

When we want to fetch the inventory we use it like so:

getInventory((err, inventory) => {
    if (err) {
        console.log('error getting the inventory :(');
    }

    else {
        // code to do whatever you want with inventory here
    }
})

 

I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs:

Quote

Don't rely on the older endpoint and the deprecated loadInventory method as it will likely be removed in the future.

 

EDIT: Callbacks are getting ugly nowadays imo so you can also use a promise if you prefer:

const getInventory = () => {
    return new Promise((resolve, reject) => {
        manager.loadInventory(730, 2, true, (err, inventory) => {
            if (err) {
                reject(err);
            } else {
                resolve(inventory);
            }
        });
    });
}

getInventory()
    .then(inventory => {
        // do whatever here
    })
    .catch(err => console.log('oops'));

// OR

// this must be inside a async function
try {
	const inventory = await getInventory();
  	// do whatever here
} catch (err) {
	console.error(err)
}

 

Edited by vrtgn
Added try catch as Doc suggested.
Posted
6 minutes ago, vrtgn said:

I also made this mistake as a beginner. 

You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory.

You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched.


const getInventory = (callback) => {
    manager.loadInventory(730, 2, true, (err, inventory) => {
        if (err) {
            callback(err);
        }

        else {
            callback(null, inventory);
        }
    })
}

I personally prefer to check for errors first, but whatever you prefer you can change it to. 

When we want to fetch the inventory we use it like so:


getInventory((err, inventory) => {
    if (err) {
        console.log('error getting the inventory :(');
    }

    else {
        // code to do whatever you want with inventory here
    }
})

 

I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs:

This makes perfect sense! I tried using a callback and it worked, but I didn't like the mess I created so I made the code in my original question. Your solution is clean and makes sense. Also I will use getInventoryContents. Thanks a bunch!

9 minutes ago, vrtgn said:

I also made this mistake as a beginner. 

You have a misunderstanding of asynchronous code in javascript. Fetching the inventory takes some time, and so by returning inventory, the variable is being returned before the program can even fetch your inventory.

You will have to create your own callback and that works by taking in a function as a callback parameter, and then executing that function once the inventory has been fetched.


const getInventory = (callback) => {
    manager.loadInventory(730, 2, true, (err, inventory) => {
        if (err) {
            callback(err);
        }

        else {
            callback(null, inventory);
        }
    })
}

I personally prefer to check for errors first, but whatever you prefer you can change it to. 

When we want to fetch the inventory we use it like so:


getInventory((err, inventory) => {
    if (err) {
        console.log('error getting the inventory :(');
    }

    else {
        // code to do whatever you want with inventory here
    }
})

 

I also saw that you are using the loadInventory method, it would be much better if you used the getInventoryContents method as it says this in the docs:

Oh but one question. getInventoryContents has a steamid param, so I'd assume you can fetch other users inventories, but there is a getUserInventoryContents function for that. What's the difference?

Posted
11 minutes ago, What Comes Around said:

Oh but one question. getInventoryContents has a steamid param, so I'd assume you can fetch other users inventories, but there is a getUserInventoryContents function for that. What's the difference?

You may have made a mistake, getInventoryContents does not have a steam ID param and is used exclusively for fetching the account's inventory, where as getUserInventoryContents like you correctly said is used to fetch other users inventories. 

Posted
2 minutes ago, vrtgn said:

You may have made a mistake, getInventoryContents does not have a steam ID param and is used exclusively for fetching the account's inventory, where as getUserInventoryContents like you correctly said is used to fetch other users inventories. 

Yes I was mistaken, my bad. Thanks again! Your code is very clean, would have taken me a while to get all the mess I had perfected.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...