2

我使用猫鼬来建立以下(简化和压缩)数据模型:

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.

问题是什么?

提前感谢您的帮助和想法。

4

1 回答 1

0

错误消息来自 Mongoose,而不是直接来自您的测试程序。最新版本的 Mongoose (0.23) 的 Mongoose.pm 的第 132 行是在建立与 MongoDB 的连接的方法中。

在此之后,我注意到您的测试代码不包含对Mongoose->db(). 这是指定要连接的数据库所必需的。添加它并尝试您的测试程序后,我还注意到您的文档在尝试从中检索属性(即标签)之前没有先保存到 MongoDB。

这是您的测试代码,其中包含我为解决问题所做的更改。关键位以行开头# Connect to database

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 $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);

my $document_tags = $doc->tags;
print Dumper $document_tags;

# Connect to database
Mongoose->db('foo');
# and save our document first
$doc->save();

# now we can retrieve the array (note, not array ref) of tags
my @tag_array = $document_tags->all();
print Dumper(\@tag_array);
于 2013-03-31T04:46:25.330 回答