[英]Meteor.js & Mongo: can you insert a new document or parameter into the user.profile from the client?
像問題所問,這可以做到嗎?
我想將用戶鏈接數組插入當前用戶的個人資料中。 我在客戶端嘗試了類似以下操作,但是沒有用:
Meteor.users.update( {_id: Meteor.userId()}, {
$set: {
profile: {
userlinks: {
owned: "_entry_id"
}
}
}
});
我也嘗試用插入替換更新,但無濟於事。
我對它可能是什么有一些懷疑:
也許我只是不知道我在說什么(最有可能)
檢查“ 流星更新文檔”,您的語法錯誤。 嘗試:
var id = Meteor.userId();
Meteor.users.update( id, {
$set: {
profile: {
userlinks: {
owned: "_entry_id"
}
}
}
});
您可以執行此操作,如果您使用的是自定義帳戶用戶界面,則用戶必須有一個配置文件字段開頭,請確保在對用戶進行身份驗證時將配置文件設置為某種內容,即使它只是一個空白對象也可以:
var options = {
username: 'some_user',
password: 'password',
email: 'email@domain.com',
profile: {}
}
Accounts.createUser(options, function(err){
if(err){
//do error handling
}
else
//success
});
如果您刪除了不安全的軟件包,則需要確保設置了Meteor.users.allow
就像是:
Meteor.users.allow({
update: function(userId, doc, fieldNames, modifier){
if(userId === doc._id && fieldNames.count === 1 && fieldNames[0] === 'profile')
return true;
}
})
因此用戶只能更新自己,並且只能更新個人資料字段。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.