Americo Americo - 5 months ago 14
JSON Question

Passing JSON Values Into POST Request - Node

I have the following index.js file;

var Lob = require('lob')('test_6afa806011ecd05b39535093f7e57757695');

var residence = require('./addresses.json');

console.log(residence.residence.length);

for (i = 0; i < residence.residence.length; i++) {
console.log(residence.residence[i].name);

Lob.addresses.create({
name: 'residence.residence[i].name',
}, function(err, res) {
console.log(err, res);
});
}


When I
console.log( residence.residence[i].name )
, the appropriate response "RESIDENT" appears.

However, I'm trying to pass in that same value as a value in the post request under
Lob.addresses.create({
which isn't recognizing it.

How do I pass in that value into my key value pair as the value for "name:"?

Answer

It appears that you are passing in a literal String 'residence.residence[i].name' because of your use of quotes... Drop the quotes:

for (i = 0; i < residence.residence.length; i++) {
    console.log(residence.residence[i].name);

    Lob.addresses.create({
        name: residence.residence[i].name,
    }, function(err, res) {
        console.log(err, res);
    });

}
Comments