0

I've been experimenting with nosql databases recently, mostly mongodb and I came to a weird blockage. To keep it really short I started implementing user auth / profile creation. User and profile have a 1-1 relationship. When a user is created, so is the profile but with no values at the start except for the user id

    user = new User({
        username,
        email,
        password
    })
    user.password = await bcrypt.hash(password, salt)

    const newUser = await user.save()

    const newProfile = await new Profile({user: newUser.id})

    await newProfile.save()

So this works just fine, I even added some checks to compare the newUser.id and newProfile.user, it's a match, they're identical

Now the problem arises when I try to update the profile in this code here, for some reason I think mongodb doesn't think the id values are the same, I've been going around this for a while and can't figure out what I'm doing wrong. So this piece of code returns NULL to my profile update endpoint and I have no idea why ( I'm using mongoose )

try {
    const profile = await Profile.findOneAndUpdate(
        {user: req.user.id}, 
        {$set: profileData},
        {new: true})

        return res.json(profile)

} catch (err) {
    
    console.log(err.message)
    res.status(500).send("Server error")
}

I was thinking if this doesn't work out, I could just generate an id before making a doc and passing it to the collections as a custom field

There are no errors

someguy
  • 115
  • 5
  • Add the errors if any – Prakash S Jul 18 '21 at 04:07
  • Hey, there doesn't seem to be any errors at all, it just won't work, I checked if profileData is maybe empty but it isn't, it works only when i set upsert to true, but that keeps creating new unlinked documents – someguy Jul 18 '21 at 11:10

0 Answers0