#[Resloved] TypeError: not a function

1 messages · Page 1 of 1 (latest)

sacred eagle
#
function() {
        let target = getPlayerWith(args[0]);
        let inv = target.getComponent("inventory").container;
        if (inv.size == inv.emptySlotsCount) return sendMsg(player, `§4${removeColorCodes(target.name)}§c's Inventory Is Empty!`);
        let msg = `  §2${removeColorCodes(target.name)}§a's Inventory:  \n\n`;
        msg += `§7§l================`;
        for (let i = 0; i < inv.size; i++) {
          let item = inv.getItem(i);
          let lore = item.getLore();
          if (item != undefined) {
            msg += `\n§e§lSLOT ${i} §r§8|| §r${removeColorCodes(item.typeId)}${item.nameTag == undefined ? "§r" : " " + item.nameTag + "§r"} §7x${item.amount}`;
            if (showEnchants == true && item.hasComponent("minecraft:enchantments")) {
              let enchants = item.getComponent("minecraft:enchantments").enchantments.getEnchantments();
              let allEnchants = enchants;
              for (let i = 0; i < allEnchants.length; i++) {
                let enchant = allEnchants[i];
                let name = enchant.type;
                let level = enchant.level;
                msg += `\n  §r§5${name} §3${level}`;
              }
            }
            if (showLore == true && lore[0]) {
              for (let i = 0; i < lore.length; i++) {
                msg += `\n    §5§o${lore[i]}`;
              }
            }
          }
        }
        msg += `§7§l================`;
        sendMsg(player, msg);
      }
#

Let me tell you something rq

#

sendMsg, getPlayerWith, showEnchants, showLore are defined but not here..

#

and all of them are being used in the rest of my code so if there was anything it should've flagged the other stuff first

#

And why im using a function expression is because im doing a cmds system.

#

And AGAIN, it has no errors

cold jackal
#

hasComponent isn't a function

sacred eagle
#

Ok thx

#

[Resvoled] TypeError: not a function

sacred eagle
#

TypeError: not a function

#

I thought it was resolved but still throws same error

cold jackal
#

What did you change?

sacred eagle
#

I changed item.hasComponent to item.getComponent

tidal olive
#

item.hasComponent is true

cold jackal
#

Could be the initial function declaration

#

function()

sacred eagle
#

What

#

Nawww

#

I use that for all my commands tho

#

Why does it not throw an error in them if its the function()

cold jackal
#

Function declarations are function funcName(params) { ... }

sacred eagle
#

Ik

#

Im using an obj for each cmd

#

And a property named data and it has a function expression

stoic hatch
#

One of the debugging practices we use at my workplace if the line number isn't given to you is just comment out large portions of code to identify where the error is coming from - then work in that narrow space once the commented out code helps identify where the error kept coming from (since it won't appear anymore).

Because, right now, it's a lot of us just shooting in the dark without more debugging done to help narrow it down on your end. Again, that's if the error isn't providing a line number.

cold jackal
#

Yours is an immediately invoked function, should be alright actually

tidal olive
#

use console.warn in multiple lines in the function to narrow the error down

sacred eagle
#

It literally only says "TypeError: not a function"

stoic hatch
#

Yeah, in that case you need to narrow down where it's coming from more.

sacred eagle
cold jackal
#

try { ... } catch (error) { console.warn(error, error.stack) }

tidal olive
#

console.warn("1")

sacred eagle
tidal olive
cold jackal
#

Stack trace shows error line

tidal olive
#

ah
didn't know that

sacred eagle
#

Uh

#

At the line i defined the inventory of the target

#

The ```js
let inv = target.getComponent("inventory").container;

#

I'm only going to assume its my getPlayerWith function but it doesn't throw any errors?

tidal olive
#

maybe it didn't return any player

cold jackal
#

Console warn target, see what pops up

stoic hatch
#

What do you get in your output when you put this before that line

console.warn(typeof target);

And

console.warn(typeof target.getComponent);

The outputs of those will give everyone better insight into what that is and what's happening further.

sacred eagle
#

Yooo it works... kinda

#

Now it picks up the same error but at the line where i defined all enchants

stoic hatch
#

Wait, it just magically started working and you didn't change any code...?

sacred eagle
#

I did change the code

#

I realized something off

#

I didn't input any if (!args[0]) at the start so target was undefined

cold jackal
sacred eagle
#

And it didn't send me any msg and stop the function

cold jackal
#

Err, it's not a thing there either

sacred eagle
#

Well

#

I dont know to interact with item enchants tbh

#

Last time i touched it was in 1.19.83

#

And never again

#

I just wanna get a list of enchants on the item

cold jackal
#

[...this.getComponent('enchantments').enchantments].map(v => ({ name: v.type.id.capitalize(), level: v.level, maxLevel: v.type.maxLevel })); is what I do to map enchants & levels

sacred eagle
#

Uh

cold jackal
#

"this" being itemstack

sacred eagle
#

In a for loop terms...

#

Because i am having lots of problems with .map and .forEach too...........

#

Ill try to use this?

#

Yeah no i don't know how to use this

stoic hatch
#

You can use the code he provided to iterate over the enchantments as a for loop, as you'd like. It's a very clean and modern ECMAScript solution.

const enchantments = [...this.getComponent('enchantments').enchantments]
  .map(v => ({ 
    name: v.type.id.capitalize(), 
    level: v.level, 
    maxLevel: v.type.maxLevel 
  }));

for (const enchantment of enchantments){
  const enchantmentName = enchantment.name;
  const enchantmentLevel = enchantment.level;
}
#

Though, for you, you'd replace this with item

sacred eagle
#

Nice thank you very much you guys for helping me out

sacred eagle
#

Nvm

cold jackal
#

Enchants components changed completely in preview - much easier to use.