This is my code in twilio functions,
exports.handler = function (context, event, callback) {
const got = require('got');
const mortgageInfo = event.mortgageInfo;
const mortgage_number = event.mortgage_number;
got('https://cyan-sparrow-7490.twil.io/assets/data6.json').then (response =>{
const mort = JSON.parse(response.body);
let mortgageData = mort;
//Begin filter code
let val = mortgageInfo;
// I think this line here is my problem
let index = mortgageData.map(function(e) { return e.mortgageid.value; }).indexOf(val);
// I think this line here is my problem
//End filter code
let mortgageSpecificData = mortgageData[index];
callback(null,mortgageSpecificData);
});
===========SAMPLE ASSET ==============
===========SAMPLE ASSET ==============
I think it can be a lot simpler than that. One thing to note is that event.mortgageInfo
is going to be a string, but in your data, the mortgageid
property is a number. So when you compare, you need to convert first.
Here's a function that I believe will work:
const got = require("got");
exports.handler = function (context, event, callback) {
const mortgageInfo = parseInt(event.mortgageInfo, 10);
got("https://cyan-sparrow-7490.twil.io/assets/data6.json").then(
(response) => {
const mortgageData = JSON.parse(response.body);
const mortgageSpecificData = mortgageData.find(
(mortgage) => mortgage.mortgageid === mortgageInfo
);
callback(null, mortgageSpecificData);
}
);
};
In this case I first change the mortgageInfo
into a number by calling parseInt
on the event.mortgageInfo
. Then, once you fetch the data and parse it, you can find the object with the matching mortgageid
using the array's find
method. find
takes a function that is called with each member of the array and returns when it evaluates to true
. In this case, for each member of the array we check whether the mortgageid
matches the mortgageInfo
we got earlier.