*alexandre* has asked for the wisdom of the Perl Monks concerning the following question:

Hi I just learning Catalyst and I got the following exception while i attempt to navigate to my url : http://localhost:3000/album I've created the following controller
package MusicApp::Controller::Album; use Moose; use namespace::autoclean; BEGIN { extends 'Catalyst::Controller' } sub base :Chained('/') :PathPart('album') :CaptureArgs(0) { my ($self, $c) = @_; $c->stash(resultset => $c->model('DB::Album')); } sub list :Chained('base') :PathPart('') :Args(0) { my ($self, $c) = @_; $c->stash(albums => [$c->stash->{resultset}->all]); } sub create :Chained('base') :PathPart('create') :Args(0) { my ($self, $c) = @_; if ($c->req->method eq 'POST') { my $album = $c->stash->{resultset}->create({ title => $c->req->params->{title}, artist => $c->req->params->{artist}, }); $c->response->redirect($c->uri_for($self->action_for('list'))) +; } } sub edit :Chained('base') :PathPart('edit') :Args(1) { my ($self, $c, $id) = @_; my $album = $c->stash->{resultset}->find($id); if ($c->req->method eq 'POST') { $album->update({ title => $c->req->params->{title}, artist => $c->req->params->{artist}, }); $c->response->redirect($c->uri_for($self->action_for('list'))) +; } $c->stash(album => $album); } sub delete :Chained('base') :PathPart('delete') :Args(1) { my ($self, $c, $id) = @_; my $album = $c->stash->{resultset}->find($id); $album->delete; $c->response->redirect($c->uri_for($self->action_for('list'))); } 1;
and created the ORM Entity Album :
# First, create the Models # lib/MusicApp/Schema/Result/Album.pm package MusicApp::Schema::Result::Album; use strict; use warnings; use base qw/DBIx::Class::Core/; __PACKAGE__->table('albums'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1, is_nullable => 0, }, title => { data_type => 'varchar', size => 128, is_nullable => 1, }, artist => { data_type => 'varchar', size => 128, is_nullable => 1, }, ); __PACKAGE__->set_primary_key('id'); __PACKAGE__->has_many(album_songs => 'MusicApp::Schema::Result::AlbumS +ong', 'album_id'); __PACKAGE__->many_to_many(songs => 'album_songs', 'song'); 1;
And the Entity Song
package MusicApp::Schema::Result::Song; use strict; use warnings; use base qw/DBIx::Class::Core/; __PACKAGE__->table('songs'); __PACKAGE__->add_columns( id => { data_type => 'integer', is_auto_increment => 1, is_nullable => 0, }, title => { data_type => 'varchar', size => 128, is_nullable => 1, }, artist => { data_type => 'varchar', size => 128, is_nullable => 1, }, length => { data_type => 'varchar', size => 16, is_nullable => 1, }, ); __PACKAGE__->set_primary_key('id'); __PACKAGE__->has_many(album_songs => 'MusicApp::Schema::Result::AlbumS +ong', 'song_id'); __PACKAGE__->many_to_many(albums => 'album_songs', 'album'); 1;
And the relational table AlbumSong :
package MusicApp::Schema::Result::AlbumSong; use strict; use warnings; use base qw/DBIx::Class::Core/; __PACKAGE__->table('album_songs'); __PACKAGE__->add_columns( album_id => { data_type => 'integer', is_nullable => 0, }, song_id => { data_type => 'integer', is_nullable => 0, }, ); __PACKAGE__->belongs_to(album => 'MusicApp::Schema::Result::Album', 'a +lbum_id'); __PACKAGE__->belongs_to(song => 'MusicApp::Schema::Result::Song', 'son +g_id'); 1; # Return true value at end of modules
I've created DB.pm file inside the directory MusicApp\Model\ folder (not sure if it's correct) :
package MusicApp::Model::DB; use strict; use base qw( Rose::DB ); __PACKAGE__->use_private_registry; __PACKAGE__->register_db( domain => __PACKAGE__->default_domain, type => __PACKAGE__->default_type, driver => 'sqlite', database => $ENV{DB_PATH} || 'mycrud.db', ); 1; >/code> My yaml config file <code> --- abstract: 'Catalyst based application' author: - 'Catalyst developer' build_requires: ExtUtils::MakeMaker: 6.36 Test::More: '0.88' configure_requires: ExtUtils::MakeMaker: 6.36 distribution_type: module dynamic_config: 1 generated_by: 'Module::Install version 1.21' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: MusicApp no_index: directory: - inc - t requires: Catalyst::Action::RenderView: 0 Catalyst::Plugin::ConfigLoader: 0 Catalyst::Plugin::Static::Simple: 0 Catalyst::Runtime: '5.90132' Config::General: 0 Moose: 0 namespace::autoclean: 0 db_path: C:\MusicApp\script\music.db resources: license: http://dev.perl.org/licenses/ version: '0.01'
What can be wrong within this exception : Caught exception in MusicApp::Controller::Album->list "Can't call method "all" on an undefined value at C:\MusicApp\lib/MusicApp/Controller/Album.pm line 14." And that correspond to the following line of code :
$c->stash(albums => [$c->stash->{resultset}->all]);
  • Comment on Catylst + SQL Caught exception in MusicApp::Controller::Album->list "Can't call method "all" on an undefined value at C:\MusicApp\lib/MusicApp/Controller/Album.pm
  • Select or Download Code

Replies are listed 'Best First'.
Re: Catylst + SQL Caught exception in MusicApp::Controller::Album->list "Can't call method "all" on an undefined value at C:\MusicApp\lib/MusicApp/Controller/Album.pm
by LanX (Saint) on Nov 23, 2024 at 19:06 UTC
    Tl;Dr , but

    > What can be wrong within this exception :   $c->stash(albums => [$c->stash->{resultset}->all]);

    $c->stash->{resultset} is undef

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      Finaly I followed this tutorial https://www.shieldui.com/blogs/perl-catalyst-integration-tutorial Thanks