Can't call method "model" on an undefined value at

653 views Asked by At

I have been building catalyst apps of a number of years now. This is the first time I am getting an error trying to open a view. When I call the view (there is only one view), I get the following error:

Can't call method "model" on an undefined value at....

The following snippet of code is used in Root.pm,. This is what is generating the error on the browser. Again, this is not the first time I am using this catalyst method to call a model.

my $model = $c->model( 'mypackage' );
my $result = $model->get_my_results();

The alternative is to use:
use mypackage;
my $model = 'mypackage';
my $results = $model->get_my_results();

Has any one ever encounter this before? If yes, can you tell me how you resolved it.

Thanks

EDITED
Please note, as indicated in the original post, this is a Perl Catalyst app. The $c is a Catalyst object. In the Root.pm file, $c is received as follows:

sub myform :Local {
my ($self, $c) = @_;
my $model = $c->model( 'mypackage' );
my $results = $model->get_my_results();
.
.
.
}

2

There are 2 answers

6
Kevin Hinshaw On

Typo? Did you mean $c->model in your first line?

0
Phil On

So, I figured out the path of my evil ways. I inadvertently forgot to pass $c in the calling method. Therefore the $c in myform() had no value. Hence, it could not find the Catalyst method called "model".

sub myform :Local {
my ($self, $c) = @_;
my $model = $c->model( 'mypackage' );
my $results = $model->get_my_results();
.
.
.
}

sub someother_method :Local {
my ($self, $c) = @_;
.
.
.
my $myform_info = $self->myform($c) <---this was missing the $c
.
}