I haven't coded in Meteor in a while, but I have this Meteor method that creates a task and returns the ID and another that appends that task to a project:
Meteor.methods({
createTask(task) {
// TODO Add a check here to validate
Tasks.insert(task, (err, id) => {
if (err) {
throw new Meteor.Error(err);
}
id = {id: id};
console.log('Returning id: ', id);
return id;
});
}
});
Meteor.methods({
appendTaskToProject(projectId, taskId) {
// TODO Add check here to validate
const project = Projects.findOne({_id: projectId});
if (project) {
if (!project.tasks) {
project.tasks = [];
}
project.tasks.push(taskId);
Projects.update({_id: projectId}, project, (err, id) => {
if (err) {
throw new Meteor.Error(err);
}
});
} else {
throw new Error("Could not find project");
}
}
});
I am attempting to call it on the client like thus:
Meteor.call('createTask', task, (err, taskId) => {
console.log('err: ', err);
console.log('taskId: ', taskId);
if (err) {
this.setState({ error: err.message});
} else {
Meteor.call('appendTaskToProject', projectId, taskId, (err, result) => {
if (err) {
this.setState({ error: err.message});
} else {
this.setState({ newTaskDialogOpen: false })
}
});
}
});
The problem I am having is that taskId
is not set in the callback. From the method-side I see the log message inthe server like:
I20180110-07:30:46.211(-5)? Returning id: { id: 'a3nS9GcRhuhhLiseb' }
And in the client:
Returning id: {id: "a3nS9GcRhuhhLiseb"}id:
Tasks.jsx:43 err: undefined
Tasks.jsx:44 taskId: undefined
So I know it's returning something, but the callback is just not getting it. I know I should probably change the createTask to just take the task AND the projectId to link it too, but I would like to try and figure out why it's not getting the results of the Meteor method into the callback on the client-side.