1

我遇到了一个有趣的挑战,不知道如何解决它。我正在从我的 mailchimp 中提取订阅者列表,并用数据填充我网站上的成员页面。我注册的字段之一是“网站或LinkedIn个人资料网址”。

我想做的是,检查他们是否提供了一个linkedin地址,如果有,将他们的url放在linkedin成员插件中,如果没有使用其他数据来创建一个更简单的个人资料、名称网站、标题等。

我的问题是我不能说是否 data.websiteField.match(/linkedin/) 在玉中,所以我要么需要将数据传递给我遇到问题的一些客户端 javascript,要么做其他事情。

这是从 mailchimp 返回的数据示例

[ // this first bit is just the fields
  [
    "email address",
    "first name",
    "last name",
    "role",
    "organization",
    "headline",
    "website",
    "areas of interest",
    "please email me about:",
    "member_rating",
    "optin_time",
    "optin_ip",
    "confirm_time",
    "confirm_ip",
    "latitude",
    "longitude",
    "gmtoff",
    "dstoff",
    "timezone",
    "cc",
    "region",
    "last_changed"
  ],
  [ // and here's the first user
    "woop@booomshakala.com", // haha, just hiding some data
    "Woop",
    "Shak",
    "Tester",
    "ShakaCo",
    "Whooooooper",
    "http://linkedin.com/in/costamichailidis", // hit me up sometime
    "Creativity, Innovation, Ideas and Science",
    "Job Placement, Internships, Recruitment & Retention, Technology & MOOCs, Measurement & Evaluation, Documentation & Dissemination",
    2,
    "2013-03-28 19:28:55",
    "173.52.122.111",
    "2013-03-28 19:29:12",
    "173.52.122.111",
    "40.7648000",
    "-73.7775000",
    "-5",
    "-4",
    "America/New_York", "US", "NY", "2013-03-28 19:29:12"
  ]
]

任何帮助都会摇滚!

另外,我正在使用快递。express 是否使本地人在客户端 JavaScript 中可用?

4

1 回答 1

2

客户端正则表达式

Jade 允许您-在行首使用修饰符执行任意 javascript

- if (profile.websiteField.match(/linkedin/)
    // render linkedin profile here
- else
    // render simple profile here

服务器端正则表达式

我认为在服务器端格式化配置文件信息并将renderLinkedIn布尔字段设置为 true 或 false会更简单

function displaySignUpPage(req, res) {
  var profile = formatMailChimpData()
  // profile now looks like
  // {
  //   "email address": "woop@booomshakala.com",
  //   "first name": "Noah",
  //   ...
  // }

  var linkedInRegex = /linkedin/;
  profile.renderLinkedIn = linkedInRegex.test(profile.website) // set renderLinkedIn to true or false
  // say your jade view is called signUpPage.jade
  var pageData = {
    title: 'Register',
    profile: profile
  }
  res.render('signUpPage', pageData)
}


function formatMailChimpData() {
  var mailChimpData = [
    [
      "email address",
      "first name",
      "last name",
      "role",
      "organization",
      "headline",
      "website"
      // other fields truncated
    ],
    [
      "woop@booomshakala.com", // haha, just hiding some data
      "Noah",
      "Isaacson",
      "Tester",
      "ShakaCo",
      "Whooooooper",
      "http://www.linkedin.com/pub/noah-isaacson/59/6a2/553"
    ]
  ]


  // mailChimp puts the keys as the first entry
  var mailChimpFields = mailChimpData[0]
  var mailChimpProfile = mailChimpData[1]
  // make profile into key-value pairs
  var keyValuePairs = mailChimpFields.map(function (field, index) {
    var profileValue = mailChimpProfile[index]
    var keyValuePair = [field, profileValue]
    return keyValuePair
   })
  var profile = keyValuePairs.reduce(function(prev, pair) {
    var key = pair[0]
    var value = pair[1]
    prev[key] = value
    return prev
  }, {})
  return profile
}
于 2013-03-30T15:51:31.567 回答