Express .map函数不返回值 [英] Express .map function not returning values

查看:134
本文介绍了Express .map函数不返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have some jquery in my view file where I want to take all of the values of the individual fields with the name, discoverySource. The solution thanks to some help from a stack user was to use the lodash module and then map the array into individual values to be appropriately used for my sequelize BulkCreate function. With my current post setup, the .map method correctly creates individual rows in my db based on the amount of fields filled in, but for some reason the values aren't being passed. I am not sure the reason this is happening because the body-parser value is correct and the values are being logged correctly everywhere outside of the variable that is being set. I commented next to each console.log to show what values are returned. My issue is that I receive undefined and [object Object] for the map method. Website 1, Website 2, Website 3, were my test values for the fields

这是我的路线:

.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>

推荐答案

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

With bulkCreate() you need to pass the array directly.

models.DiscoverySource.bulkCreate(sources)

模式是:

Model.bulkCreate(<array_of_values);

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

这篇关于Express .map函数不返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆