简体   繁体   中英

Meteor.js & Mongo: can you insert a new document or parameter into the user.profile from the client?

Like the questions asks, can this be done?

I would like to insert an array of a user's links into the current user's profile. I tried something like following on the client side but it did not work:

Meteor.users.update( {_id: Meteor.userId()}, {
        $set: {
            profile: {
                userlinks: {
                    owned: "_entry_id"
                }
            }
        }
    });

I also tried replacing update with insert but to not avail.

I have a couple of suspicions about what it could be:

  • I am not setting the mongodb permissions correctly (least likely)
  • I am not publishing the user collection correctly (I currently don't publish it at all so I assume meteor does this automatic... but maybe not enough of it?) (somewhat likely)
  • Insert is the correct command but this is restricted to the server, so I have to put the insert function inside of a Meteor method on the server and then call that method from the client? (more likely)

Or maybe I just have no idea what I am talking about (most likely)

Check Meteor Update Docs your syntax is wrong. Try:

var id = Meteor.userId();
 Meteor.users.update( id, {
        $set: {
            profile: {
                userlinks: {
                    owned: "_entry_id"
                }
            }
        }
    });

You can do that, your user needs to have a profile field to start with if you're using a custom account UI make sure you set the profile to something when you authenticate a user even if it's just a blank object to start with:

var options = {
    username: 'some_user',
    password: 'password',
    email: 'email@domain.com',
    profile: {}
}

Accounts.createUser(options, function(err){
    if(err){
        //do error handling
    }
    else
        //success
});

if you've removed the insecure package you'll need to make sure you setup the Meteor.users.allow

something like:

Meteor.users.allow({
    update: function(userId, doc, fieldNames, modifier){
        if(userId === doc._id && fieldNames.count === 1 && fieldNames[0] === 'profile')
            return true;
    }
})

so that the user can only update themselves and they can only update the profile field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM