【问题标题】:dealing with 'undefined' properties in Meteor.method处理 Meteor.method 中的“未定义”属性
【发布时间】:2017-06-29 19:25:11
【问题描述】:

我在 Meteor 应用程序中创建了一个函数,该函数设置为“丰富”流星集合中包含的数据。该函数旨在遍历集合,利用fullcontact API 提取数据库中维护的所有客户条目的附加数据(即LinkedIn Bio;员工人数等)。

问题在于并非所有数据点都可用于集合中的所有元素(例如,客户可能没有 LinkedIn 个人资料)。该函数适用于最初的几个元素,但最终无法抛出 TypeError: Cannot read property '2' of undefined,因为 data variable 不包含公司的 LinkedIn 个人资料简介(对于这个特定示例)。

您对锻炼有什么建议?有任何想法吗?非常感谢您的帮助 - 我已经为此工作了几个小时。

Meteor.methods({
  enrichment() {
    var fullcontact = new FullContact(Meteor.settings.fullContact);
    for (var i = 1; i < customerDb.find({ company: "Qualify" }).count(); i++) {
      var url = customerDb.findOne( { company: "Qualify", 'item.clientId': i.toString() } )['item']['contact_website'];
      var data = fullcontact.company.domain(url);
      if ( data['status'] == 200 ) {
        customerDb.update ({ 
          company: "Qualify", 'item.clientId': i.toString()
        }, {
          $push: {
            bio: data['socialProfiles'][2]['bio'],
            keywords: data['organization']['keywords'],
            employees: data['organization']['approxEmployees'],
            domesticTrafficRank: data['traffic']['topCountryRanking'][0],
            globalTrafficRank: data['traffic']['ranking'][0]
          }
        });
      } else {
        console.log('Data could not be found on the company')
      }
    }
  }
});

【问题讨论】:

  • 如果data['socialProfiles'][2] 不存在,是什么阻止您检查是否存在?喜欢(data['socialProfiles'][2] || {})['bio']?或者还有其他地方可以获取简历吗?
  • 嗨@chazsolo:我想我大致理解你的意思 - 但我试图避免在我的方法中构建多个if (typeof data['socialProfiles']['2'] === 'undefined' || variable === null){} else 语句的情况 - 我希望更干净,更优雅的东西。另外-我不知道如何检查meteor.update函数中的存在(它会起作用吗?)。感谢您的快速回复 - 感谢您对此的想法!菲利普
  • 在处理嵌套属性时,您必须以一种或另一种方式检查对象是否存在。您可以内联(如我上面建议的那样)或在customerDb.update 调用之前进行。这样,您可以在调用之前构建要推送的对象,然后您会得到类似:$push: updatedCustomerData
  • @chazsolo 谢谢你 - 这是有道理的,我明天试试(后者)并让你知道!
  • 运气好吗?如果您需要示例,我很乐意为您发布答案

标签: meteor fullcontact


【解决方案1】:

根据@chazsolo 的建议,您可以使用javascript AND 和OR 处理可能丢失的数据和键。这是一种常见的防御性编码模式。

如果缺少任何父键或数组元素,则此处的每个项目都将替换为空字符串。如果您对可能缺少的内容有更多了解,则可以简化此操作。此外,在某些情况下,您可能需要数字而不是字符串。

if ( data['status'] == 200 ) {
  const bio = data['socialProfiles'] && data['socialProfiles'][2] && data['socialProfiles'][2]['bio'] || '';
  const keywords = data['organization'] && data['organization']['keywords'] || '';
  const employees = data['organization'] && data['organization']['approxEmployees'] || '',
  const domesticTrafficRank = data['traffic'] && data['traffic']['topCountryRanking'] && data['traffic']['topCountryRanking'][0] || '',
  const globalTrafficRank = data['traffic'] && data['traffic']['ranking'] && data['traffic']['ranking][0] || '';

  customerDb.update (
    { company: "Qualify", 'item.clientId': i.toString() },
    { $push: { bio, keywords, employees, domesticTrafficRank, globalTrafficRank }}
  });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-11
    • 2011-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多