Express .map函数不返回值

时间:2016-02-08 22:10:41

标签: jquery express sequelize.js lodash

我的视图文件中有一些jquery,我想要使用名称discoverySource获取各个字段的所有值。由于堆栈用户的一些帮助,解决方案是使用lodash模块,然后将数组映射到单个值,以适合我的sequelize BulkCreate函数。使用我当前的帖子设置,.map方法根据填写的字段数量正确地在我的数据库中创建单独的行,但由于某种原因,值未被传递。我不确定发生这种情况的原因是因为正文解析器值是正确的,并且正在正在设置的变量之外的任何地方正确记录值。我在每个console.log旁边注释,以显示返回的值。我的问题是,我为地图方法收到了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>

2 个答案:

答案 0 :(得分:1)

此: -

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

答案 1 :(得分:1)

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

models.DiscoverySource.bulkCreate(sources)

模式是:

Model.bulkCreate(<array_of_values);

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

相关问题