User.set add more data to specified field ( set sets one value)

Hi, user.set() sets a value, but how to add new value to specified place in user.arttributes? Like to array and push new items?

Thx!

Hi legenuspl,

The Moralis.User object inherits from Moralis.Object so all the same rules apply. See the docs on Objects here:
https://docs.moralis.io/objects

Cheers.

Ok, so I managed to write to User.(“tokens”) new values by checking which are already there and downloading them plus adding new one and then setting new value of the array with
user.set(“tokensAdded”, item);
await user.save();

where item is a new Array of elements, is there a simpler method to push data to that object? I saw that it can be done like on accounts linking it adds new account to “accounts” array

If a property is an array you can treat it like any other array.

const MyObject = Moralis.Object.extend("MyObject");
const obj = new MyObject();
obj.set("items", []);
obj.get("items").push("foo");

// or set items all at once
obj.set("items", ["foo", "bar"]);

// or assuming items has already been set, store it in a variable and manipulate that
const items = obj.get("items");
items.push("tar") // this will produce an error if items is undefined

https://docs.moralis.io/objects#data-types

Thx, now i understand!

1 Like