当我更新我的集合时,在本地集合中显示doc { _id: undefined }
,重新加载页面后此文档消失。我尝试使用方法和客户端操作 - 结果相同
其中一个js错误: 预计会找到已删除的文件
如果我再次更新集合,那么js会给出另一个错误: 重复_id' undefined'
这是我的代码:
Template.depForm.events({
'submit #dep_form': function(event) {
var action, dep, form;
event.preventDefault();
form = event.target;
dep = $(form).serializeJSON();
if (dep.status != null) {
dep.status["new"] = dep.status["new"] ? true : false;
dep.status.hidden = dep.status.hidden ? true : false;
}
action = this.editDep ? 'update' : 'create';
return DepartmentsCollection["" + action + "Dep"](dep, function(err) {
var msg;
if (err) {
return alertify.error(err.reason);
} else {
msg = action.charAt(0).toUpperCase() + action.slice(1);
alertify.success(i18n("deps.dep" + msg + "Success"));
return Router.go("deps");
}
});
}
});
和收藏:
depTitleObj = {};
_.each(i18n.getAvailableLanguages(), function(v) {
return depTitleObj[v] = {
type: String
};
});
depTitleSchema = new SimpleSchema(depTitleObj);
depStatusSchema = new SimpleSchema({
hidden: {
type: Boolean,
optional: true
},
"new": {
type: Boolean,
optional: true
}
});
depSchema = SimpleSchema.build(SimpleSchema.timestamp, {
title: {
type: depTitleSchema
},
synonyms: {
type: String
},
use_count: {
type: Number,
optional: true,
min: 0
},
main_dep: {
type: String,
optional: true
},
status: {
type: depStatusSchema,
optional: true
}
});
Departments = new Mongo.Collection('departments');
Departments.attachSchema(depSchema);
allow = function(userId) {
if (!userId) {
return false;
}
return UsersCollection.findOne({
_id: userId
}, {
fields: {
role: 1
}
}).hasAccess('admin');
};
Departments.allow({
insert: function(userId, doc) {
return allow(userId);
},
update: function(userId, doc) {
return allow(userId);
},
remove: function(userId, doc) {
return allow(userId);
}
});
_.extend(Departments, {
createDep: function(data, cb) {
return this.insert(data, cb);
},
updateDep: function(data, cb) {
var depId;
if (!data._id) {
return cb(new Meteor.Error("dep-notfound", i18n("deps.errorUpdateNoDepID")));
}
depId = data._id;
delete data._id;
this.update(depId, {
"$set": data
}, cb);
return true;
}
});
this.DepartmentsCollection = Departments;
})();
我使用了underscore.js _。omit()的函数来消除表单对象中的属性_id,但它没有帮助,然后我手动将对象更新,这也是没有帮助。所有这些都暗示了minimongo中存在错误的想法。
DepartmentsCollection 中的updateDep 函数:
updateDep: function(data, cb) {
var dep, depId;
if (!data._id) {
return cb(new Meteor.Error("dep-notfound", i18n("deps.errorUpdateNoDepID")));
}
depId = data._id;
dep = _.omit(data, "_id");
console.log(__indexOf.call(dep, "_id") >= 0 ? "true" : "false");
this.update(depId, {
"$set": {
synonyms: dep.synonyms
}
}, cb);
return true;
}
这是form.serializeJSON()
答案 0 :(得分:0)
您emptyText
的位置 - 这是将表单字段转换为您插入dep = $(form).serializeJSON();
正确的对象?表格上是否有DepartmentsCollection
字段?也许您可以在那里显示现有记录的_id
。但是因为你将所有表单字段推送到对象中(如果它最初没有在创建时设置),那么它将存在于对象中但是未定义。你必须这样做:
_id
避免在创建部门时尝试设置未定义的if ( !this.editDep ) delete dep._id;
。
这有点推测,因为我无法看到您的表单或您拥有的字段,但根据您报告的错误它是有意义的。
答案 1 :(得分:0)
我想出了这种行为的原因。事实是,字段main_dep - 对同一集合中的文档的引用以及在客户端显示附加文档,我将此文档作为主要样本的结果添加。当我刚刚将main_dep添加为主文档的属性时,问题就会自动消失