CONTEXT
我使用Mantra架构与Meteor 1.3和Astronomy来管理我的馆藏。我试图通过Meteor回调函数传递插入对象的id,以便在我的容器中使用它。
问题 当我在控制台中记录元素时,我得到了正确的ID,但在尝试控制台从较高层(从保存我的动作调用Meteor方法调用的文件)进行控制时,得到了未定义的ID。然后我需要将它从我的动作传递给使用它的Container。
方法(在服务器上)
export default function () {
Meteor.methods({
'condition.create'(conditionTitle, conditionType, segmentId) {
const condition = new Conditions();
// Show the latency compensations
Meteor._sleepForMs(500);
// XXX: Do some user authorization
// website._id = _id;
condition.conditionTitle = conditionTitle;
condition.segmentId = segmentId;
condition.conditionType = conditionType;
condition.save(function(err, id) {
const conditionId = id;
return conditionId;
});
}
});
}
操作文件(调用的地方 - 客户端)
export default {
create({Meteor, LocalState, FlowRouter}, conditionTitle, conditionType, segmentId) {
LocalState.set('CREATE_CONDITION_ERROR', null);
if (!conditionTitle) {
return LocalState.set('CREATE_CONDITION_ERROR', 'Condition Title is required!');
}
if (!conditionType) {
return LocalState.set('CREATE_CONDITION_ERROR', 'Condition Type is required!');
}
if (!segmentId) {
return LocalState.set('CREATE_CONDITION_ERROR', 'segmentId is required.');
}
LocalState.set('CREATE_CONDITION_ERROR', null);
Meteor.call('condition.create', conditionTitle, conditionType, segmentId, function(err, conditionId) {
if (err) {
return LocalState.set('CREATE_CONDITION_ERROR', err.message);
} else {
console.log("method" + conditionId);
}
});
},
clearErrors({LocalState}) {
return LocalState.set('CREATE_CONDITION_ERROR', null);
}
};
容器(客户端上的部分代码段 - 这是我需要使用conditionId的地方)
onSubmit(event) {
event.preventDefault;
const {create, segmentId} = this.props;
const conditionTitle = this.state.conditionTitle;
const conditionType = this.state.conditionType;
create(conditionTitle, conditionType, segmentId, function(conditionId) {
console.log(conditionId);
});
}
答案 0 :(得分:1)
这个问题与Mantra没有任何关系。我在Astronomy .save函数中返回插入的对象,但没有将该值传递给整个Meteor方法。因此未定义。
这是服务器上的正确方法:
Meteor.methods({
'condition.create'(conditionTitle, conditionType, segmentId) {
const conditionAstro = new Conditions();
// Show the latency compensations
Meteor._sleepForMs(500);
// XXX: Do some user authorization
// website._id = _id;
conditionAstro.conditionTitle = conditionTitle;
conditionAstro.segmentId = segmentId;
conditionAstro.conditionType = conditionType;
const condition = conditionAstro.save(function(err, id) {
// const conditionId = id;
console.log(id);
return id;
});
return condition;
}
});