Sigma-Web-Dev-Course icon indicating copy to clipboard operation
Sigma-Web-Dev-Course copied to clipboard

in password manager with each entry i save the previous one gets deleted

Open akchita-16 opened this issue 10 months ago • 4 comments

#130 i have followed your code completely but at the end when we add passwords in pssOP( mongodb ) the previously added password gets deleted

akchita-16 avatar Feb 27 '25 19:02 akchita-16

i am getting the same issue

VarunSaxena07 avatar Mar 22 '25 12:03 VarunSaxena07

Hi, you can remove strict mode in main.jsx it worked for me

Nakul003 avatar Apr 01 '25 06:04 Nakul003

Possible reasons:

  • The backend is probably not creating a new document for each password.
  • Maybe it's using findOneAndUpdate() or a fixed _id, so it keeps overwriting.
  • Also, React 18 Strict Mode may cause double rendering. Removing Strict Mode in main.jsx might help in testing.

Solution: Make sure every password entry creates a new document in MongoDB, not replace the old one. Hope this helps!

Gunjan-pandey85340 avatar Aug 02 '25 13:08 Gunjan-pandey85340

#130 i have followed your code completely but at the end when we add passwords in pssOP( mongodb ) the previously added password gets deleted

update save password as -

const savePassword = async () => {
  if (form.site.length > 3 && form.username.length > 3 && form.password.length > 3) {
    // if form has an id, update
    if (form.id) {
      await fetch("http://localhost:3000/edit", { 
        method: "PUT", 
        headers: { "Content-Type": "application/json" }, 
        body: JSON.stringify(form) 
      });

      // update frontend state
      setpasswordArray(passwordArray.map(p => 
        p.id === form.id ? { ...form } : p
      ));

      toast('Password Updated', {
        position: "top-right",
        autoClose: 5000,
        theme: "dark"
      });
    } else {
      // else create new
      const newPassword = { ...form, id: uuidv4() };
      await fetch("http://localhost:3000/", { 
        method: "POST", 
        headers: { "Content-Type": "application/json" }, 
        body: JSON.stringify(newPassword) 
      });

      setpasswordArray([...passwordArray, newPassword]);

      toast('Password Saved', {
        position: "top-right",
        autoClose: 5000,
        theme: "dark"
      });
    }

    setform({ site: "", username: "", password: "" });
  } else {
    toast('Error: Password not saved!');
  }
};

then update editpassword -

const editPassword = (id) => {
  const selected = passwordArray.find(i => i.id === id);
  if (selected) {
    setform({ ...selected });  // keep id in form
  }
};

then add an api in server.js -

app.put('/edit', async (req, res) => { 
    const { id, site, username, password } = req.body;
    const db = client.db(dbName);
    const collection = db.collection('passwords');

    try {
        const result = await collection.updateOne(
            { id }, // find record by custom id
            { $set: { site, username, password } } // update only contents
        );

        res.send({ success: true, result });
    } catch (error) {
        console.error("Edit error:", error);
        res.status(500).send({ success: false, error: error.message });
    }

});

and it will work if you like my solution please follow me on github

Prateek-GitProjects avatar Aug 21 '25 18:08 Prateek-GitProjects