fix calling doc.toObject when doc is undefined