Skip to content

getMongoData.js – Wrong handling of strings that look like dates #47

@drmirror

Description

@drmirror

In getMongoData.js, any string that is parseable as an ISODate is converted into { $date : ... } in the output. This is wrong, I am in fact seeing fields that contain arbitrary alphanumeric codes being converted into $date notation. This is apparently done by this section in the code:

// For use in JSON.stringify to properly serialize known types
function jsonStringifyReplacer(k, v){
    if (v instanceof ObjectId)
        return { "$oid" : v.valueOf() };
    if (v instanceof NumberLong)
        return { "$numberLong" : longmangle(v) };
    if (v instanceof NumberInt)
        return v.toNumber();
    // For ISODates; the $ check prevents recursion
    if (typeof v === "string" && k.startsWith('$') == false){
        try {
            iso = ISODate(v);
            return { "$date" : iso.valueOf() };
        }
        // Nothing to do here, we'll get the return at the end
        catch(e) {}
    }
    return v;
}

Actually, I don't think it's right to parse strings into ISODates at all here, but I don't understand the overall logic well enough. Intuitively, I would think that v instanceof ISODate should suffice here.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions