louischatriot / nedb

The JavaScript Database, for Node.js, nw.js, electron and the browser
MIT License
13.46k stars 1.03k forks source link

Update creates a duplicate with exact same _id #702

Open shivardev opened 8 months ago

shivardev commented 8 months ago

I tried to update a document which already exists

This is the function

async function appendChat(chatData: ChatContent) {
  try {
    const db = new Datastore({
      filename: "src/Db/UserChats.db",
      autoload: true,
    });

    const condition = { from: chatData.from, to: chatData.to };
    db.findOne(condition, (err: Error | null, userChat: any) => {
      if (err) {
        console.error("Error while searching for userChat:", err);
      } else if (!userChat) {
        // If userChat doesn't exist, create a new one
        const newUserChat: TChat = {
          from: chatData.from,
          to: chatData.to,
          ChatContent: [chatData],
          // Add other properties as needed
        };

        // Insert the new userChat
        db.insert(
          newUserChat,
          (insertErr: Error | null, insertedUserChat: Chat) => {
            if (insertErr) {
              console.error("Error while inserting userChat:", insertErr);
            } else {
              console.log("New userChat created:", insertedUserChat);
            }
          }
        );
      } else {
        console.log("UserChat already exists:", userChat);
        console.log(userChat._id);
        db.update(
          { _id: userChat._id },
          { $push: { ChatContent: chatData } },
          {},
          (updateErr: Error | null, numUpdated: Chat) => {
            if (updateErr) {
              console.error("Error while updating userChat:", updateErr);
            } else {
              console.log("UserChat updated with new content:", numUpdated);
            }
          }
        );
      }
    });
  } catch (err) {
    console.error("Error hashing password:", err);
  }
}

instead of updating the one that exists already. it creates a new object and saves it same _id

{
        "from": "EmailSample007@gmail.com",
        "to": "EmailSample@gmail.com",
        "ChatContent": [
            {
                "audioURL": "uploads/audios/1697701185128-w2g0y7-audiofile.m4a",
                "from": "EmailSample007@gmail.com",
                "to": "EmailSample@gmail.com",
                "isVoice": true,
                "msg": ""
            }
        ],
        "_id": "ilypBnrGLe4bITnH"
    }
    {
        "from": "EmailSample007@gmail.com",
        "to": "EmailSample@gmail.com",
        "ChatContent": [
            {
                "audioURL": "uploads/audios/1697701185128-w2g0y7-audiofile.m4a",
                "from": "EmailSample007@gmail.com",
                "to": "EmailSample@gmail.com",
                "isVoice": true,
                "msg": ""
            },
            {
                "audioURL": "uploads/audios/1697701435156-td7c1b-audiofile.m4a",
                "from": "EmailSample007@gmail.com",
                "to": "EmailSample@gmail.com",
                "isVoice": true,
                "msg": ""
            }
        ],
        "_id": "ilypBnrGLe4bITnH"
    }

Please help me solve it. Thankyou

sujeking commented 7 months ago

The same to me,help

shivardev commented 7 months ago

i didn't get any solution rather I have dropped using the NEDB and moved to MongoDB local,

shivardev commented 7 months ago

The same to me,help

As this version is not maintained or supported maybe try using the forked supported version https://github.com/seald/nedb