Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I am doing the example in the catalyst book (Catalyst.Accelerating.Perl.Web.Application.Development) and i have this error: Caught exception in AddressBook::Controller::Address->edit "Can't call method "find" on an undefined value at /path/to/the/controller what is the problem? I am new to catalyst and perl thanks
  • Comment on Can't call method "find" on an undefined value

Replies are listed 'Best First'.
Re: Can't call method "find" on an undefined value
by CountZero (Bishop) on Jan 12, 2012 at 19:56 UTC
    You will have to show the script that gives you this error.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

      here is the code
      package AddressBook::Controller::Address; use strict; use warnings; use base qw(Catalyst::Controller::FormBuilder Catalyst::Controller::Bi +ndLex); use Data::Dumper; sub edit : Local Form { my ($self, $c, $address_id, $person_id) = @_; my $address : Stashed; print $c->request->arguments->[0]; print "\n"; print Dumper(" address_id is: ".Dumper($address_id)); print Dumper("person_id is: ".Dumper($person_id)); my $persid = $c->model('AddressDB::Addresses')->find({person => $id}); my $person = $c->model('AddressDB::People')->find({id => $persid}); if ($self->formbuilder->submitted && $self->formbuilder->validate){ # transfer data from form to database $address->location($self->formbuilder->field('location')); $address->postal ($self->formbuilder->field('postal' )); $address->phone ($self->formbuilder->field('phone')); $address->email ($self->formbuilder->field('email')); $address->insert_or_update; $c->stash->{message} = ($address_id > 0 ? 'Updated ' : 'Added new ').'address for '. $ +address->person->name; $c->detach('list'); } else { # transfer data from database to form if(!$address_id){ $c->stash->{message} = 'Adding a new address '; } else { $c->stash->{message} = 'Updating an address '; } $c->stash->{message} .= ' for '. $address->person->name; $self->formbuilder->field(name => 'location', value => $address +->location); $self->formbuilder->field(name => 'postal', value => $address-> +postal); $self->formbuilder->field(name => 'phone', value => $address->p +hone); $self->formbuilder->field(name => 'email', value => $address->e +mail); } }
      thanks
        the problem is in this line  my $persid = $c->model('AddressDB::Addresses')->find({person => $id});
Re: Can't call method "find" on an undefined value
by stonecolddevin (Parson) on Jan 25, 2012 at 20:38 UTC

    Your result class is called "package AddressBook::Schema::AddressDB::Result::Address;", but you're calling "Addresses". Try changing that in your model call.

    Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past

      it should be $address = $c->model('AddressDB::Address')->find({id => $address_id});

        If it's the primary key you don't have to specify the column name

        Three thousand years of beautiful tradition, from Moses to Sandy Koufax, you're god damn right I'm living in the fucking past