簡體   English   中英

Meteor.js和Mongo:您可以從客戶端將新文檔或參數插入到user.profile中嗎?

[英]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"
                }
            }
        }
    });

我也嘗試用插入替換更新,但無濟於事。

我對它可能是什么有一些懷疑:

  • 我沒有正確設置mongodb權限(極有可能)
  • 我沒有正確發布用戶集合(我目前根本不發布它,所以我認為流星會自動運行...但是可能不夠用嗎?) (有點可能)
  • Insert是正確的命令,但這僅限於服務器,因此我必須將insert函數放在服務器上的Meteor方法內部,然后從客戶端調用該方法? (更傾向於)

也許我只是不知道我在說什么(最有可能)

檢查“ 流星更新文檔”,您的語法錯誤。 嘗試:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM