我正在尝试使用Dart,MongoDB和Objectory构建一个用于学习目的的小应用程序。为此,我创建了一个基本模型:
part of myapp;
class Member extends PersistentObject {
String get username => getProperty('username');
set username(String value) => setProperty('username',value);
String get password => getProperty('password');
set password(String value) => setProperty('password',value);
// more fields here
}
void registerClasses() {
objectory.registerClass(Member, () => new Member());
}
在我的主app文件中,我有:
library myapp;
import "package:redstone/server.dart" as app;
import "package:objectory/objectory.dart";
import "dart:core";
part "lib/member.dart";
Objectory objectory;
void main() {
objectory = new Objectory("mongodb://localhost/myapp", registerClasses, false);
objectory.initDomainModel().then((_) {
app.setupConsoleLog();
app.start(address: "127.0.0.1", port: 8080);
});
}
当我运行它时,我得到了以下错误:
Breaking on exception: object of type NoSuchMethodError
Unhandled exception:
The null object does not have a method 'dataMapDecorator'.
NoSuchMethodError: method not found: 'dataMapDecorator'
Receiver: null
Arguments: [Instance of '_LinkedHashMap']
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:45)
#1 BasePersistentObject.BasePersistentObject (package:objectory/src/persistent_object.dart:10:46)
#2 PersistentObject.PersistentObject (package:objectory/src/persistent_object.dart:1:1)
#3 Member.Member (file:///Users/lucian/Projects/dart/eviq/bin/lib/member.dart:1:1)
#4 registerClasses.<anonymous closure> (file:///Users/lucian/Projects/dart/eviq/bin/lib/member.dart:74:45)
#5 Objectory.registerClass (package:objectory/src/objectory_base.dart:118:39)
#6 registerClasses (file:///Users/lucian/Projects/dart/eviq/bin/lib/member.dart:74:26)
#7 Objectory.initDomainModel (package:objectory/src/objectory_base.dart:141:28)
#8 main (file:///Users/lucian/Projects/dart/eviq/bin/eviq.dart:13:28)
#9 _startIsolate.isolateStartHandler (dart:isolate-patch/isolate_patch.dart:216)
#10 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:124)
知道我做错了什么吗?感谢。
答案 0 :(得分:2)
您正在设置基础Objectory类。对于服务器端和浏览器环境,有Objectory的具体实现。
假设您尝试在服务器端使用Objectory,则应使用ObjectoryDirectConnectionImpl
请参阅https://github.com/vadimtsushko/objectory/blob/master/example/console/blog_console.dart例如
library blog_example;
import 'package:objectory/objectory_console.dart';
import '../domain_model/domain_model.dart';
const Uri = 'mongodb://127.0.0.1/objectory_blog';
main(){
objectory = new ObjectoryDirectConnectionImpl(Uri,registerClasses,true);
var authors = new Map<String,Author>();
var users = new Map<String,User>();
objectory.initDomainModel().then((_) {
ObjectoryDirectConnectionImpl
构造函数命令对象的第三个参数,用于在打开db后删除集合 - 对于样本和测试有用,您可能应该将它false