我使用猫鼬来建立以下(简化和压缩)数据模型:
package Model::Tag;
use Moose;
use Mongoose::Class; with 'Mongoose::Document';
has 'value' => (is => 'rw', isa => 'Str', required => 1);
no Moose;
__PACKAGE__->meta->make_immutable;
package Model::Document;
use Moose;
use Mongoose::Class; with 'Mongoose::Document';
has 'title' => (is => 'rw', isa => 'Str', required => 1);
has_many 'tags' => (is => 'rw', isa => 'Model::Tag');
no Moose;
__PACKAGE__->meta->make_immutable;
我的测试的重要部分是:
package main;
use strict;
use warnings;
use Data::Dumper;
my $expected; my $got;
my $doc = Model::Document->new(title => 'My new document with many tags');
my $tag1 = Model::Tag->new(value => 'foo');
my $tag2 = Model::Tag->new(value => 'bar');
my $x1 = $doc->tags->add($tag1);
my $x2 = $doc->tags->add($tag2);
# print "x1 = $x1 x2 = $x2 \n";
my $document_tags = $doc->tags;
print Dumper $document_tags;
can_ok($document_tags, 'all');
my $tag_array_ref = $document_tags->all();
现在的问题:
$document_tags 的转储输出是一个 Mongoose::Join 对象:
$VAR1 = bless( {
'delete_buffer' => {},
'with_class' => 'Model::Tag',
'buffer' => {
'58102804' => bless( {
'value' => 'foo'
}, 'Model::Tag' ),
'58069732' => bless( {
'value' => 'bar'
}, 'Model::Tag' )
}
}, 'Mongoose::Join' );
关于Mongoose::Join的文档列出了方法:
add, remove, find, find_one, first, all, hash_on, hash_array, query, collection, with_collection_name
但打电话
$document_tags->all();
导致错误
Can't use an undefined value as a HASH reference at C:/Perl/site/lib/Mongoose.pm line 132.
问题是什么?
提前感谢您的帮助和想法。