Fix a bug when writing documents.