redstone-dart / redstone

A metadata driven microframework for Dart.
http://redstone-dart.github.io/redstone
MIT License
342 stars 42 forks source link

Error when MongoDB.findOne doesn't find anything #63

Closed cgarciae closed 9 years ago

cgarciae commented 9 years ago

This code that tries to add a user to mongo

@app.Route("/user/add", methods: const[app.POST])
addUser2(@app.Attr() MongoDb dbConn, @Decode() User user) 
{
    user.username = user.username.trim();

    return dbConn.findOne('user', User, {"username": user.username}) //ERROR
    .then((value) {...});

gives me this error

MapperException: User#id: The null object does not have a method '[]'.

NoSuchMethodError: method not found: '[]'
Receiver: null
Arguments: ["_id"]

package:redstone_mapper/mapper_factory.dart 358:11  _getOrCreateMapper.
package:redstone_mapper/src/mapper_impl.dart 32:26  _TypeDecoder.convert
package:redstone_mapper/mapper.dart 321:28          GenericTypeCodec.decode
package:redstone_mapper_mongo/manager.dart 101:22   MongoDb.findOne.

I resorted to using MongoDB.collection (...).findOne (...) as it returns a null object when if doesn't find anything. Also, is there a way to use Redstone Mapper to convert a Map to an Object and viceversa? Would really help in this case.

luizmineo commented 9 years ago

Thanks for reporting this, I`ll update the mongodb wrapper to handle null values.

To just encode or decode objects, you can use the MongoDb.encode() and MongoDb.decode() methods

luizmineo commented 9 years ago

Fixed in redstone_mapper v0.1.11