【发布时间】:2015-07-26 12:26:11
【问题描述】:
我有一个集合,我只想在 Collection.find().count() === 0 时插入数据。我正在使用流星方法进行插入,这样我也可以插入 Meteor 用户 ID,我的最终目标是在该特定集合上运行更新函数,我开始工作了,但是执行初始插入的方法继续运行即使它是 if 语句中的包装器,仅在集合为空时才这样做。
所有内容都是发布和订阅的,还有 Financials.allow 和 deny 代码,以及带有 ownsDocument 的权限文件。如果需要,我会发布所有代码。
同样在控制台中我收到此错误,尽管该方法正在插入数据,即使集合有一个文档,我只希望它在它为空时插入,因此几乎可以进行全新安装.
提前感谢您的帮助。
Exception while simulating the effect of invoking 'financialUserId' TypeError: Cannot read property '_id' of undefined {stack: (...), message: "Cannot read property '_id' of undefined"} TypeError: Cannot read property '_id' of undefined
at Meteor.methods.financialUserId (http://localhost:3000/lib/collections/financials.js?0fde44b180e856bc334a164ad9859e394fd9578d:22:19)
at http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4269:25
at _.extend.withValue (http://localhost:3000/packages/meteor.js?43b7958c1598803e94014f27f5f622b0bddc0aaf:955:17)
at _.extend.apply (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4260:54)
at _.extend.call (http://localhost:3000/packages/ddp.js?d1840d3ba04c65ffade261f362e26699b7509706:4138:17)
at http://localhost:3000/client/templates/editable/client_edit.js?578e3f500b0c73d3d22e659540d653cb6309cd52:9:8
at http://localhost:3000/client/templates/editable/client_edit.js?578e3f500b0c73d3d22e659540d653cb6309cd52:15:3
客户端文件夹
/client/editable/client_edit.js
if (Meteor.isClient) {
if (Financials.find().count() === 0) {
var financial = {issuedOutstanding: 666} ;
Meteor.call('financialUserId', financial, function(error, result) {});
}
}
/client/editable/edit_financials.js
Template.financialEdit.events({
'submit form': function(e) {
e.preventDefault();
var currentFinanceId = this._id;
var financialsProperties = {
issuedOutstanding: $('#issuedOutstanding').val()
};
Financials.update(currentFinanceId, {$set: financialsProperties}, function(error) {
if (error) {
console.log(currentFinanceId);
console.log(error);
alert(error.reason);
} else {
console.log(financialsProperties);
// Router.go('financials');
//Router.go('financials');
Router.go('financials', {_id: currentFinanceId});
}
});
}
});
Lib 文件夹
/lib/collections/financials.js
Meteor.methods({
financialUserId: function(eventAttributes) {
var user = Meteor.user();
var financial = _.extend(eventAttributes, {
userId: user._id
});
var financialId = Financials.insert(financial);
return {
_id: financialId
};
}
});
【问题讨论】:
标签: javascript mongodb meteor meteor-collections