meteor-astronomy icon indicating copy to clipboard operation
meteor-astronomy copied to clipboard

Field assign in server side method notworking

Open dibrovadev opened this issue 5 years ago • 8 comments

Hello.

Astronomy version: 2.5.2

I have client side and server side parts of my model.

in client side (common model without platform restrictions visible on both client and server) I define all fields:

const Order = Class.create({
  name: ....,
  collection: ....,
  fields:{
    ....
    direction: {
      type: String,
      optional:true,
    },
    ....
  }
})

on server side I define in most cases method:

Order.extend({
  meteorMethods:{
      setDirection(){
        this.direction = 'hello';
      },
  }
});

Anywhere in server side code I do this


const order = new Order({...some fields});
order.setDirection();
console.log(order);

as result in console I see this output

Class {
  ....,
  direction: unbdefined,
  ....,
}

When I move setDirection from methods to helpers all works perfect.

Am I doing anything wrong, but looks like a bug?

dibrovadev avatar Sep 18 '18 07:09 dibrovadev

Additional

If I do it in this way

      setDirection(){
        this.direction = 'hello';
        console.log(this);
      },

I see correct value of direction field

dibrovadev avatar Sep 18 '18 08:09 dibrovadev

You have to define fields in both environments client and server. It will not work when you only define them in one of them

lukejagodzinski avatar Sep 18 '18 09:09 lukejagodzinski

Sorry for confusing, I have common model that doesn't have any restrictions and another one that visible only on the server

dibrovadev avatar Sep 18 '18 09:09 dibrovadev

Ok, so what your doing wrong is using meteorMethods instead of helpers. For such cases where you only want to set values, you use helpers. Meteor methods would be used in cases where you just want to invoke something in the client and make sure that the same method is invoked on the server.

lukejagodzinski avatar Sep 18 '18 09:09 lukejagodzinski

So you mean that it is impossible to set any fields inside function that was defined inside meteorMethods.

Maybe it should be documented anywhere

dibrovadev avatar Sep 18 '18 12:09 dibrovadev

I said, use helpers for that

lukejagodzinski avatar Sep 18 '18 13:09 lukejagodzinski

I thought the whole point of the meteorMethods is to make sure the same code runs on both client and server at the same time? So surely if the same code is being run and is setting the same variables on both client and server, why can't you set any fields in methods?

lucaciraolo avatar Jun 28 '19 15:06 lucaciraolo

You can set fields values inside the method. If you have some reproduction repository a I can help solve your problem

lukejagodzinski avatar Jun 29 '19 10:06 lukejagodzinski