Client show different log than server

Hi guys! something weird happened to me yesterday and id like to know if anyone can explain why it happen.
Im using react with meteor. In client i called the meteor method like that:

 Meteor.call('tasks.update', {'jobos':fields.jobos, material:true})

on the server i have this method

'tasks.update'(valor) {
    check(valor, Object);
    const taskId = Tasks.findOne({'jobos':valor.jobos});

    console.log('1',valor, taskId)
    if (taskId === undefined) {
      console.log('2',valor, taskId)
      throw new Meteor.Error('TASK DIDNT FOUND');
    }else{
      console.log('3',valor, taskId)
      Tasks.update(taskId, { $set: valor });
    }
  }

when i execute the method i receive different log back on either client and server .
On the client (chrome) it logs:

1 {jobos: “000001.0”, material: true} undefined tasks.js:62
2 {jobos: “000001.0”, material: true} undefined tasks.js:64
meteor.js?hash=857dafb4b9dff17e29ed8498a22ea5b1a3d6b41d:1061 Exception while simulating the effect of invoking ‘tasks.update’ errorClass {isClientSafe: true, error: “TASK DIDNT FOUND”, reason: undefined, details: undefined, message: “[TASK DIDNT FOUND]”, …}

On the DOS meteor server it logs:

1 { jobos: ‘000001.0’, material: true } { _id: ‘rBT3RH3sZdrS52oFZ’,
jobos: ‘000001.0’,
verba: true,
prestacao: false,
trello: false,
nf: false,
material: false }
3 { jobos: ‘000001.0’, material: true } { _id: ‘rBT3RH3sZdrS52oFZ’,
jobos: ‘000001.0’,
verba: true,
prestacao: false,
trello: false,
nf: false,
material: false }

and my mongo updates.

The question is, why the same console.log its showing different information on both sides.
by the way my mongo was updated.

Can anyone help me understand that?

thank you!!!

Hi @bilicio, welcome to the Meteor forums!

Your task with jobos: "000001.0" does not exist on the client. Your server can find it and update it, but your client cannot find that task.

Your server needs to publish that document and your client needs to subscribe to it:

Also, your taskId constant is actually the full task object, not just the _id, therefore your Tasks.update(taskId...) line is putting unnecessary load on the server since it will be checking that all fields match the existing fields before it applies the update. I suggest renaming this variable and using it like so:

'tasks.update'(value) {
    // First do you need to check that a user is logged in, or can anyone change your data?
    // check someone is logged in:
    check(this.userId, String);
    // also check that they have the right permissions?

    // Next, check that the client is providing valid parameters:
    check(valor, {
      jobos: String,
      // check ALL the fields the client provides, otherwise they can change anything to anything.
      material: Match.Optional(Boolean),
      // can the user change any other properties of the Task?
      // If so, validate the values here...
    });

    // rename taskId to task, since findOne returns the object not the _id.
    // plus only fetch the fields you really need:
    const task = Tasks.findOne({jobos: valor.jobos}, {fields: {jobos: 1});
  
    if (!task) {
      throw new Meteor.Error('TASK DIDNT FOUND');
    }else{
      Tasks.update(valor.jobos, { $set: valor);
    }
  }
2 Likes

oh my god @wildhart! huge valuable information here! i have 10 collections and im using all like that!
you clarified my mind in how to build a perfect communication with the server! thank you so much! :grimacing:

1 Like