jagi / meteor-astronomy

Model layer for Meteor

Home Page:https://atmospherejs.com/jagi/astronomy

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Field assign in server side method notworking

dibrovadev opened this issue · 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?

Additional

If I do it in this way

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

I see correct value of direction field

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

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

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.

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

Maybe it should be documented anywhere

I said, use helpers for that

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?

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