cphill February 2016

Express .map function not returning values

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>
  {{> h        

Answers


BenG February 2016

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


Ben Polge February 2016

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

Post Status

Asked in February 2016
Viewed 3,875 times
Voted 4
Answered 2 times

Search




Leave an answer