yortus/DBFFile

dbf-file.js appendRecordsToDBF sets value to empty space for DateTime field

michaelmok2021 opened this issue · 1 comments

I notice this line of code which cause issue for us when trying to insert a row into a dbf with empty datetime field. utils_2.formatVfpDateTime throws exception saying getTime is not a function which is correct when the datetime value is artificially set to empty space.

Can you consider making a code change to skip the datetime field when value is null? Thanks.

 // Write the records.
        for (let i = 0; i < records.length; ++i) {
            // Write one record.
            let record = records[i];
            validateRecord(dbf.fields, record);
            let offset = 0;
            buffer.writeUInt8(0x20, offset++); // Record deleted flag
            // Write each field in the record.
            for (let j = 0; j < dbf.fields.length; ++j) {
                // Get the field's value.
                let field = dbf.fields[j];
                let value = record[field.name];
                if (value === null || typeof value === 'undefined')
                    value = '';
                let encoding = getEncodingForField(field, dbf._encoding);
                // Encode the field in the buffer, according to its type.
                switch (field.type) {
                    case 'C': // Text
                        let b = iconv.encode(value, encoding);
                        for (let k = 0; k < field.size; ++k) {
                            let byte = k < b.length ? b[k] : 0x20;
                            buffer.writeUInt8(byte, offset++);
                        }
                        break;
                    case 'N': // Number
                    case 'F': // Float - appears to be treated identically to Number
                        value = value.toString();
                        value = value.slice(0, field.size);
                        while (value.length < field.size)
                            value = ' ' + value;
                        iconv.encode(value, encoding).copy(buffer, offset, 0, field.size);
                        offset += field.size;
                        break;
                    case 'L': // Boolean
                        buffer.writeUInt8(value ? 0x54 /* 'T' */ : 0x46 /* 'F' */, offset++);
                        break;
                    case 'T': // DateTime
                        const { julianDay, msSinceMidnight } = utils_2.formatVfpDateTime(value);

Thanks @michaelmok2021, this should work now in v1.6.0.