fix calling doc.toObject when object is undefined