If the user does not have a collection while saving to the nft table, I want to create an automatic collection beforesave and save it to the nft table, but it saves undefined. Where am I going wrong, can you help?
// cloud code
Moralis.Cloud.beforeSave("Nfts", async function(request) {
const userCollection = request.object.get("collections");
if (userCollection) {
logger.info("success");
} else {
logger.info("fail");
const UserCollection = Moralis.Object.extend("UserCollections");
const collection = new UserCollection();
collection.set("userId", request.object.get("userId"));
collection.set("title", "UntitledCollection");
collection.set("description", "My first collection");
collection.save(null, { useMasterKey: true }).then((usercollection) => {
request.object.set("collections",usercollection);
});
}
});
// react
const Nfts = Moralis.Object.extend("Nfts");
const nft = new Nfts();
nft.set("category", category);
nft.set("properties", propertyData);
nft.set("collections", collection); // collection is "" in frontend. i want to set on cloud before save
.......
nft.save().then((nft) => {
toast.success('Created successfully!',options);
}, (error) => {
toast.error('Unsuccessful!',options);
alert('Failed to create new object, with error code: ' + error.message);
});
it returns; schema mismatch for Nfts.collections; expected Pointer but got String