Javascript Meteor:如何保存到集合中并从中获取数据?

Javascript Meteor:如何保存到集合中并从中获取数据?,javascript,mongodb,meteor,Javascript,Mongodb,Meteor,我试着做两个函数。 Save()应检查是否存在该用户的现有文档,如果存在,则使用新文档更新其保存,如果没有,则使用用户的唯一id作为文档唯一id插入新文档。 Load()应检查是否存在具有用户Id的现有存储,并加载它。 我对这一点完全陌生,这里是我得到的错误 未捕获错误:不允许。不受信任的代码只能更新 按ID分类的文件[403] 我知道这是因为更新和插入是如何工作的。但是我想对文档使用用户的唯一iD,因为它看起来很简单 function Save() { if (Meteor.u

我试着做两个函数。 Save()应检查是否存在该用户的现有文档,如果存在,则使用新文档更新其保存,如果没有,则使用用户的唯一id作为文档唯一id插入新文档。 Load()应检查是否存在具有用户Id的现有存储,并加载它。 我对这一点完全陌生,这里是我得到的错误

未捕获错误:不允许。不受信任的代码只能更新 按ID分类的文件[403]

我知道这是因为更新和插入是如何工作的。但是我想对文档使用用户的唯一iD,因为它看起来很简单

function Save() {
        if (Meteor.userId()) {
            player = Session.get("Player");
            var save = {    
                    id: Meteor.userId(),
                    data = "data"
                    };
            console.log(JSON.stringify(save));
                if (Saves.find({id: Meteor.userId()})){
                    Saves.update( {id: Meteor.userId()}, {save: save} )
                    console.log("Updated saves")
                }
                else {
                    Saves.insert(save)
                }

            console.log("Saved");
            }
}

function Load(){
        if (Meteor.userId()){
            if (Saves.find(Meteor.userId())){
                console.log(JSON.stringify(Saves.find(Meteor.userId()).save.player));
                player = Saves.find(Meteor.userId()).save.player;
                data= Saves.find(Meteor.userId()).save.data

            }
        }
}

对象/文档
id
-字段称为
\u id

在客户端尝试更新现有对象/文档时,会发生此错误。 您始终需要传入对象
\u id
以从客户端代码更新对象/文档。 请注意,您总是尝试传递
id
而不是
\u id

因此,请像这样尝试:

function Save() {
    if (Meteor.userId()) {
        player = Session.get("Player");
        var save = {    
                _id: Meteor.userId(),
                data = "data"
                };
        console.log(JSON.stringify(save));
            if (Saves.find({_id: Meteor.userId()})){
                Saves.update( {_id: Meteor.userId()}, {save: save} )
                console.log("Updated saves")
            }
            else {
                Saves.insert(save)
            }

        console.log("Saved");
        }
}
还请注意,您的
Load()
函数可以工作,因为
Collection.find()
使用您传递的字符串作为文档的
\u id

希望有帮助