简体   繁体   中英

MongoDB Javascript replace() fails with "TypeError: Cannot call method 'replace' of undefined"

I have a collection that contains customer contact data but some fields need to be cleaned up and special characters removed.

This is the script I wrote ( list of fields has been edited for brevity )

// VARs to calculate date difference - I am sure there is a better way
var v_curr_timestamp = new Date();
var v_curr_day = v_curr_timestamp.getDate();
var v_curr_month = v_curr_timestamp.getMonth();
var v_curr_year = v_curr_timestamp.getFullYear();
var v_stop_date = new Date(v_curr_year, v_curr_month, v_curr_day, 0, 0, 0, 0);
v_diff_days = 1;
var v_start_date = new Date(v_curr_year, v_curr_month, v_curr_day - v_diff_days, 0, 0, 0, 0);

// CSV file header row
print("\"_id\"|\"UserID\"...|\"GroupIDs\"");

// Create cursor
var cursor = db.contacts.find({
  $and: [{
      UpdatedAt: {
        $gte: Date.parse(v_start_date) / 1000
      }
    },

    {
      UpdatedAt: {
        $lt: Date.parse(v_stop_date) / 1000
      }
    }
  ]
}).limit(100); // limit the number of returned documents - testing only

while (cursor.hasNext()) {
    var record = cursor.next();
    var NoteClean = record.Note.replace(/\n/g, '');
  print(
    "\"" + record._id + "\"|" +
    "\"" + record.UserID + "\"|" +
...
    "\"" + NoteClean + "\"|" +
...
    "\"" + record.GroupIDs + "\""
  );
}

When I run it from the command line

mongo localhost:27000/queue_xyz get_contacts_cleanup.js > contacts.csv

I get this error in the CSV file

"_id"|"UserID"|"PhoneNumber"|"Source"|"PrivateLabelID"|"OptOut"|"Blocked"|"Deleted"|"Note"|"CreatedAt"|"UpdatedAt"|"FirstName"|"LastName"|"Email"|"Custom1"|"Custom2"|"Custom3"|"Custom4"|"Custom5"|"GroupIDs"
"5e4f63cf404dfb30c51b0403"|"568665"|"3379628807"|"1"|"1"|"undefined"|"undefined"|"undefined"|""|"1582261199"|"1582261199"|"Colby"|"Mczeal"|""|"undefined"|"undefined"|"undefined"|"undefined"|"undefined"|"[object Object]"
2020-02-21T11:43:07.323-0500 E QUERY    TypeError: Cannot call method 'replace' of undefined
    at get_contacts_cleanup.js:31:30 at get_contacts_cleanup.js:31
failed to load: get_contacts_cleanup.js

Why it is unloading the first 2 rows but fails after that. What am I missing here?

The "crash" is due to the fact that document in my collection that has no "Note" value so running Note.replace when Note isn't a string will not work. I re-wrote the cursor iteration piece like so

...while (cursor.hasNext()) {
        var record = cursor.next();
        var Note = record.Note ? record.Note.replace(/[\n\r]+/g, '') : '';
  print(
    "\"" + record._id + "\"|" +
...
    "\"" + record.Note + "\"|" +
...

I downloaded 10k documents without error and I am going through data validation. So far, so good.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM