Express .map function not returning values

故事扮演 提交于 2019-12-11 10:42:49

问题


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

Here is my route:

.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);
    });
});

Here is my view:

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

回答1:


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

models.DiscoverySource.bulkCreate(sources)

The pattern is:

Model.bulkCreate(<array_of_values);

as documented here: http://sequelize.readthedocs.org/en/latest/api/model/#bulkcreaterecords-options-promisearrayinstance




回答2:


This:-

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

creates an object with 2 properties, discoverySource and organizationId.

So sources is an array of objects. [object Object],[object Object],[object Object]

This:-

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

is undefined because you are looking for discoverySource and organizationId on the array, and not on an object in the array.

try:-

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


来源:https://stackoverflow.com/questions/35280335/express-map-function-not-returning-values

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!