Express .map 函数不返回值

Express .map function not returning values

我的视图文件中有一些 jquery,我想在其中获取名称为 discoverySource 的各个字段的所有值。由于堆栈用户的一些帮助,解决方案是使用 lodash 模块,然后将数组映射到单独的值,以适当地用于我的 sequelize BulkCreate 函数。使用我当前的 post 设置,.map 方法根据填写的字段数量正确地在我的数据库中创建单独的行,但由于某些原因,这些值没有被传递。我不确定发生这种情况的原因,因为 body-parser 值是正确的,并且在设置的变量之外的任何地方都正确记录了这些值。我在每个 console.log 旁边注释以显示返回的值。我的问题是我收到 map 方法的 undefined[object Object]。网站 1、网站 2、网站 3 是我对字段的测试值

这是我的路线:

.post(function(req, res){
        console.log(req.body.discoverySource); //[ 'Website 1', 'Website 2', 'Website 3' ]

    var sources = _.map(req.body.discoverySource, function (source) {
        return {
             discoverySource: source,
             organizationId: req.body.organizationId
        };
    });
    console.log("These are the" + sources); //These are the[object Object],[object Object],[object Object]

    console.log(sources.discoverySource); //undefined
    console.log(sources.organizationId); //undefined
    models.DiscoverySource.bulkCreate(sources)
    .then(function(){
        return models.DiscoverySource.findAll();
    }).then(function(discoverySource){
        console.log(discoverySource);
        res.redirect('/app');
    }).catch(function(error){
        res.send(error);
        console.log('Error during Post: ' + error);
    });
});

这是我的观点:

<!DOCTYPE html>

<head>
  {{> head}}
</head>

<body>
  {{> navigation}}
  <div class="container">
    <div class="col-md-6 col-md-offset-3">
      <form action="/app/sign-up/organization" method="post">
        <p>{{user.email}}</p>
        <input type="hidden" name="admin" value="{{user.email}}">
        <input type="hidden" name="organizationId">
        <label for="sign-up-organization">Test Name</label>
        <input type="text" class="form-control" id="sign-up-organization" name="organizationName" value="" placeholder="Company/Organization">
        <a href="#" id="sign-up-add-discovery-source">Add Another</a>
        <div id="sign-up-organization-discovery-source">
          <input type="text" id="discovery-source-field" placeholder="Discovery Source" name="discoverySource[0]">
        </div>
        <br />
        <button type="submit">Submit</button>
      </form>
      <a href="/login">Already have an account? Login here!</a>
    </div>
  </div>
  <script type="text/javascript">
    $(function() {
      var dataSourceField = $('#sign-up-organization-discovery-source');
      var i = $('#sign-up-organization-discovery-source p').size();
      var sourceCounter = 1;

      $('#sign-up-add-discovery-source').on('click', function() {
        $('<p><label for="discovery-source-field"><input type="text" id="discovery-source-field" size="20" name="discoverySource[{' + sourceCounter+++'}]" value="" placeholder="Discovery Source" /></label> <a href="#" class="remove">Remove</a></p>').appendTo(dataSourceField);
        i++;
        return false;
      });
      $('#sign-up-organization-discovery-source').on('click', '.remove', function() {
        if (i > 1) {
          $(this).parent('p').remove();
          i--;
        }
        return false;
      });
    });
  </script>
</body>

这个:-

return {
     discoverySource: source,
     organizationId: req.body.organizationId
};

创建一个具有 2 个属性的对象,discoverySourceorganizationId

所以sources是一个对象数组。 [object Object],[object Object],[object Object]

这个:-

console.log(sources.discoverySource); //undefined
console.log(sources.organizationId); //undefined

undefined,因为您要查找数组中的 discoverySourceorganizationId,而不是数组中的对象。

尝试:-

console.log(sources[0].discoverySource); //discoverySource on 1st object in the array
console.log(sources[0].organizationId); //organizationIdon 1st object in the array

使用 bulkCreate() 时,您需要直接传递数组。

models.DiscoverySource.bulkCreate(sources)

模式是:

Model.bulkCreate(<array_of_values);

如此处所述:http://sequelize.readthedocs.org/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance