Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB Javascript replace()失败,返回“0”;TypeError:无法调用方法';更换';“未定义”的定义;_Javascript_Mongodb - Fatal编程技术网

MongoDB Javascript replace()失败,返回“0”;TypeError:无法调用方法';更换';“未定义”的定义;

MongoDB Javascript replace()失败,返回“0”;TypeError:无法调用方法';更换';“未定义”的定义;,javascript,mongodb,Javascript,Mongodb,我有一个包含客户联系人数据的集合,但有些字段需要清理并删除特殊字符 这是我写的脚本(为简洁起见,编辑了字段列表) 当我从命令行运行它时 mongo localhost:27000/queue_xyz get_contacts_cleanup.js > contacts.csv 我在CSV文件中得到了这个错误 "_id"|"UserID"|"PhoneNumber"|"Source"|"PrivateLabelID"|"OptOut"|"Blocked"|"Deleted"|"Note"|

我有一个包含客户联系人数据的集合,但有些字段需要清理并删除特殊字符

这是我写的脚本(为简洁起见,编辑了字段列表)

当我从命令行运行它时

mongo localhost:27000/queue_xyz get_contacts_cleanup.js > contacts.csv
我在CSV文件中得到了这个错误

"_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
为什么它卸载前两行,但在卸载后失败。我在这里遗漏了什么?

之所以出现“崩溃”,是因为我的收藏中的文档没有“Note”值,所以运行
Note。如果Note不是字符串,则替换
将不起作用。我像这样重新编写了游标迭代块

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

我下载了10k文档,没有错误,我正在进行数据验证。到目前为止,还不错。

注释
未定义?@Chris我想第三个注释中可能是
未定义
。尝试记录您得到的内容并检查它。
...while (cursor.hasNext()) {
        var record = cursor.next();
        var Note = record.Note ? record.Note.replace(/[\n\r]+/g, '') : '';
  print(
    "\"" + record._id + "\"|" +
...
    "\"" + record.Note + "\"|" +
...