Boo
Boo

Reputation: 155

Meteor.users : $addToset in Profile [nested object]

Each time a user subscribe to a "street" or another "user" I want to add their Id to a nested object in The profile of the current User.

I have try the following with different results :

Meteor.users.update(Meteor.userId(), {
        $addToSet: {
            'profile.subscription': { Street : streetDis
            }
        }
})

This update the profile , but create an entry into the array each time :

Profile.subscription[0] :  Street : "eziajepozjaoeja" 
Profile.subscription[1] : Street : "eezapoezkaejz"
Profile.subscription[2] : User : "akzejpazjepza"

The architecture I want would be as follow :

Profile.subscription.Street[0] : "eziajepozjaoeja"
Profile.subscription.Street[1]:"eezapoezkaejz"
Profile.subscription.User[0]: "akzejpazjepza"

So I try :

Meteor.users.update(Meteor.userId(), {
            $addToSet: {
                'a.profile.last.Adress': "akzejpazjepza"}
            }
    )

Which return : update failed: Access denied

Meteor.users.update(Meteor.userId(), {
            $addToSet: {
                'a.profile': { last: {Adress : "akzejpazjepza"}}
            }
    })

This would also return : update failed: Access denied

Upvotes: 0

Views: 217

Answers (1)

David Weldon
David Weldon

Reputation: 64342

If you want profile.subscription.streets and profile.subscription.users to be each be an array of ids, then you should update the user's document like this:

Meteor.users.update(Meteor.userId(), {
  $addToSet: {
    'profile.subscription.streets': streetId,
    'profile.subscription.users': userId
  }
});

Upvotes: 1

Related Questions