Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Field assign in server side method notworking #686

Open
dibrovadev opened this issue Sep 18, 2018 · 8 comments
Open

Field assign in server side method notworking #686

dibrovadev opened this issue Sep 18, 2018 · 8 comments

Comments

@dibrovadev
Copy link

dibrovadev commented Sep 18, 2018

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
Copy link
Author

Additional

If I do it in this way

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

I see correct value of direction field

@lukejagodzinski
Copy link
Member

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

@dibrovadev
Copy link
Author

dibrovadev commented Sep 18, 2018

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

@lukejagodzinski
Copy link
Member

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.

@dibrovadev
Copy link
Author

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

Maybe it should be documented anywhere

@lukejagodzinski
Copy link
Member

I said, use helpers for that

@lucaciraolo
Copy link

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?

@lukejagodzinski
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants