in password manager with each entry i save the previous one gets deleted
#130 i have followed your code completely but at the end when we add passwords in pssOP( mongodb ) the previously added password gets deleted
i am getting the same issue
Hi, you can remove strict mode in main.jsx it worked for me
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.jsxmight help in testing.
Solution: Make sure every password entry creates a new document in MongoDB, not replace the old one. Hope this helps!
#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