我有一个集合,我只希望在Collection.find().count() === 0
时插入数据。我使用meteor方法进行插入,这样我也可以插入Meteor userId,我的最终目标是在该特定Collection上运行更新函数,我开始工作,但是初始插入的方法一直在运行即使它是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
};
}
});
答案 0 :(得分:0)
尝试将financials.js移动到您的/ server目录中,或将Meteor.methods代码包装在" if(Meteor.isServer)"条件,以确保它在服务器上运行:
if (Meteor.isServer){
Meteor.methods({
financialUserId: function(eventAttributes) {
var user = Meteor.user();
var financial = _.extend(eventAttributes, {
userId: user._id
});
var financialId = Financials.insert(financial);
return {
_id: financialId
};
}
});
}
如果要在客户端上运行该方法,请使用Meteor.userId()获取userId的值。