Failed to load resource: the server responded with a status of 400 (Bad Request) moralis.js:25228 Uncaught (in promise) Error: A duplicate value for a field with unique values was provided at handleError (moralis.js:25228)

Main.js:

MoralisWeb3 = serverUrl = “https://w6j9c9lnnoio.usemoralis.com:2053/server”; //Server url from moralis.io

MoralisWeb3 = appId = “wZsf07rYLfsPudepzTdjvrAsYfgWAQb0BFbnTFJI”; // Application id from moralis.io

async function initializeApp() {

hideElement(userInfo);

hideElement(createItemForm);

await Moralis.start({ serverUrl, appId });

await Moralis.enableWeb3();

currentUser = Moralis.User.current();

if (!currentUser){

   currentUser = await Moralis.Web3.authenticate();

}

}

initUser = async () => {

const user = await Moralis.User.current();
if (await Moralis.User.current()) {

    hideElement(userConnectButton);

    showElement(userProfileButton);

    showElement(openCreateItemButton);

}else {

    showElement(userConnectButton);

    hideElement(userProfileButton);

    showElement(openCreateItemButton);

}

}

login = async () => {

try {

    await Moralis.Web3.authenticate();

initUser();

} catch (error) {

    alert(error)

}

}

logout = async () => {

await Moralis.User.logOut();

hideElement(userInfo)

initUser();

}

openUserInfo = async (email, username) => {

user = await Moralis.User.current();

if (user){

const email = user.get('email');

if (email) {

  userEmailField.value = email;

} else {

  userEmailField.value = "";

}

userUsernameField.value = user.get(‘username’);

const userAvatar = user.get(‘avatar’);

if(userAvatar){

userAvatarImg.src = userAvatar.url();

showElement(userAvatarImg);

}else{

hideElement(userAvatarImg);

}

showElement(userInfo);

}else{

login();

}

}

saveUserInfo = async () => {

user.set(‘email’, userEmailField.value);
user.set(‘username’, userUsernameField.value);

if (userAvatarFile.files.length > 0) {

const avatar = new Moralis.File("avatar.jpg", userAvatarFile.files[0]);

user.set('avatar', avatar);

}

await user.save();

alert(‘Changes saved successfully!’);

openUserInfo();

}

createItem = async () => {
if (createItemFile.files.length == 0) {
alert(“Please Select A File”);
return;
} else if (createItemNameField.value.length == 0) {
alert(“Please Name Your Item”);
return;
}

const nftFile = new Moralis.File(“nftFile.jpg”, createItemFile.files[0]);
await nftFile.saveIPFS();

const nftFilePath = nftFile.ipfs();
const nftFileHash = nftFile.hash();

const metadata = {
name: createItemNameField.value,
description: createItemDescriptionField.value,
nftFilePath: nftFilePath,
nftFileHash: nftFileHash
}

const nftFileMetadataFile = new Moralis.File(“metadata.json”, {base64 : btoa(JSON.stringify(metadata))});
await nftFileMetadataFile.saveIPFS();

const nftFileMetadataFilePath = nftFileMetadataFile.ipfs();
const nftFileMetadataFileHash = nftFileMetadataFile.hash();

const Item = Moralis.Object.extend(“Item”);
const item = new Item();
item.set(‘name’, createItemNameField.value);
item.set(‘description’, createItemDescriptionField.value);
item.set(‘nftFilePath’, nftFilePath);
item.set(‘nftFileHash’, nftFileHash);
item.set(‘metadataFilePath’, nftFileMetadataFilePath);
item.set(‘metadataFileHash’, nftFileMetadataFileHash);

await item.save();
console.log(item);

}

hideElement = (element) => element.style.display = “none”;

showElement = (element) => element.style.display = “block”;

const userConnectButton = document.getElementById(“btn-login”);

userConnectButton.onclick = login;

const userProfileButton = document.getElementById(“btn-profile”);

userProfileButton.onclick = openUserInfo;

const userInfo = document.getElementById(“userInfo”);

const userUsernameField = document.getElementById(“txtUsername”);

const userEmailField = document.getElementById(“txtEmail”);

const userAvatarImg = document.getElementById(“imgAvatar”);

const userAvatarFile = document.getElementById(“fileAvatar”);

document.getElementById(“btn-CloseUserInfo”).onclick = () => hideElement(userInfo);

document.getElementById(“btn-logout”).onclick = logout;

document.getElementById(“btn-SaveUserInfo”).onclick = saveUserInfo;

const createItemForm = document.getElementById(“createItem”);

const createItemNameField = document.getElementById(“txtcreateItemName”);
const createItemDescriptionField = document.getElementById(“txtcreateItemDescription”);
const createItemPriceField = document.getElementById(“numCreateItemPrice”);
const createItemStatusField = document.getElementById(“selectCreateItemStatus”);
const createItemFile = document.getElementById(“fileCreateItemFile”);

const openCreateItemButton = document.getElementById(“btn-openCreateItem”);
openCreateItemButton.onclick = () => showElement(createItemForm);
document.getElementById(“btn-CloseCreateItem”).onclick = () => hideElement(createItemForm);

document.getElementById(“btn-CreateItem”).onclick = createItem;

initializeApp();

do you know in what function, at what line it generated that error?

it is saying that the error is generated in Moralis not my code, this happened when I switched to Ubuntu but I reinstalled all the node_modules but it is still giving this error

if this runs in browser, I think that it should not depend on what you have in node_modules

what do you do before you get that error?

nothing, i just copy and pasted from windows to Ubuntu with a USB and decided to test it, it gave this error, i then reinstalled node_modules but that didnot help either

that is all that you get in your browser console?

yup that is all i get

can you try to add some debugging lines to see how much code executes?

I got this error today after written a cloud function @GodSlayer436,

have you tried a cloud function before getting the error? There should be smth missing there, if you reply to this also would help me, thanks.

it can happen for example if you try to reuse same email for multiple users

1 Like