Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update creates a duplicate with exact same _id #702

Open
shivardev opened this issue Oct 19, 2023 · 3 comments
Open

Update creates a duplicate with exact same _id #702

shivardev opened this issue Oct 19, 2023 · 3 comments

Comments

@shivardev
Copy link

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
Copy link

sujeking commented Dec 6, 2023

The same to me,help

@shivardev
Copy link
Author

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

@shivardev
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants