0

在我尝试插入时删除不安全的包后,它只会创建一个只有和 _id 的新记录

在服务器上,我允许在 Customers.collection 上插入

Customers.allow({
    insert: function(userID) {
        console.log(userID === userID);
        return userID === userID;
    }
});

在客户端上,我调用 insert 并将用户 ID 和表单数据传递给它

Template.tabs.events({
'submit form#customer' : function (event) {

    console.log(event.type + event.currentTarget);

    if (event.type === 'click' || event.type === 'submit') {

        event.preventDefault();

        var name = $("#name").val();
        var address = $("#address").val();
        var city = $("#city").val();
        var state = $("#state").val();
        var zip = $("#zip").val();
        var phone = $("#phone").val();
        var fax = $("#fax").val();

        doc = {user_id: this.userID, name: name, address: address, city: city, state: state, zip: zip, phone: phone, fax: fax}

        if(Customers.insert(this.userID, doc)) {
            console.log("Inserted");
            $("#name").val(null);
            $("#address").val(null);
            $("#city").val(null);
            $("#state").val(null);
            $("#zip").val(null);
            $("#phone").val(null);
            $("#fax").val(null);
        }
    }
}
});

我还尝试将插入包装在流星方法中,并从客户端进行方法调用,而不是使用相同的结果。

这是来自客户端的方法和调用

Meteor.methods({
    newCustomer: function (userID, record) {
        Customers.insert(userID, record);
                    console.log("Inserted");
    }
});

并在客户端而不是插入语句我做如下。

Meteor.call("newCustomer", this.userID, doc);

我无法从流星文档中找出任何其他解决方案,试图让它发挥作用。

4

1 回答 1

2

问题看起来是这一行:

if(Customers.insert(this.userID, doc)) {

您插入的文档应该是这样的,插入的文档是参数

if(Customers.insert(doc)) {

并且您的允许功能需要检查实际文档:

Customers.allow({
    insert: function(userID,doc) {
        return userID === doc.user_id;
    }
});

还要更改您的文档所有者,this.userId用于在服务器上发布或Meteor.methods. 要在其他地方获取登录用户的用户 ID,请使用Meteor.userId

doc = {user_id: Meteor.userId(), name: name, address: address, city: city, state: state, zip: zip, phone: phone, fax: fax}
于 2013-03-28T19:37:51.497 回答