1

tengo un problema, hay que lograr una respuesta concreta de una API que mide riesgo cardiovascular, le paso por get una consulta con los parámetros y me hace conexión, pero en la respuesta obtengo un null, los parámetros que le paso salen undefined y obtengo un null en la respuesta, quiero saber como resolverlo; No debo recibir un null, sino un resultado correcto.

El problema que tengo es que al hacer una consulta no obtengo respuesta correcta, sino un null.

Os paso la consulta:

http://localhost:3000/irc?weight=80&height=1.8&gender=male&race=aa&smoker=false&hdl=100&age=45&totCholesterol=130&sysBP=90&hipertensive=false&diabetic=false

y esto es lo que recibo

{
    "potencialRisk": null
}

A ver si me podéis ayudar, os paso código.

código de ./lib/patient.js

   const descriptions = require('../weightDescriptions.json');

const readDescriptionIRC = (lang, index) => descriptions[lang][index];

module.exports = class Patient {
    constructor(weight, height, patientInfo) {
        this.weight = weight;
        this.height = height;
        this.patientInfo = patientInfo
    }

    // public methods

    computePotentialRisk(reductions, score) {
            let computedScore;
            let lowestScore;
            let reducedTotalScore = 0;
            if (score === 'ten') {
                computedScore = this.computeTenYearScore(this.patientInfo);
                lowestScore = this.computeLowestTenYear();
            } else {
                computedScore = this.computeLifetimeRisk(this.patientInfo);
                lowestScore = this.computeLowestLifetime();
            }
            for (let i = 0; i < reductions.length; i += 1) {
                if (reductions[i] === 'statin') {
                    reducedTotalScore += (computedScore * 0.25);
                } else if (reductions[i] === 'sysBP') {
                    const sysBPCalculation = computedScore - (computedScore *
                        (0.7 ** ((this.patientInfo.systolicBloodPressure - 140) / 10)));
                    reducedTotalScore += sysBPCalculation;
                } else if (reductions[i] === 'aspirin') {
                    reducedTotalScore += (computedScore * 0.1);
                } else if (reductions[i] === 'smoker') {
                    reducedTotalScore += (computedScore * 0.15);
                }
            }
            if (Math.round((computedScore - reducedTotalScore) * 10) / 10 <= lowestScore) {
                return Math.round((computedScore - lowestScore) * 10) / 10;
            }
            return Math.round(reducedTotalScore * 10) / 10;
        }

    computeTenYearScore(patientInfo) {
        if (patientInfo.age < 40 || patientInfo.age > 79) { return null; }

        const lnAge = Math.log(patientInfo.age);
        const lnTotalChol = Math.log(patientInfo.totalCholesterol);
        const lnHdl = Math.log(patientInfo.hdl);
        const trlnsbp = patientInfo.relatedFactors.hypertensive ?
            Math.log(parseFloat(patientInfo.systolicBloodPressure)) : 0;
        const ntlnsbp = patientInfo.relatedFactors.hypertensive ?
            0 : Math.log(parseFloat(patientInfo.systolicBloodPressure));
        const ageTotalChol = lnAge * lnTotalChol;
        const ageHdl = lnAge * lnHdl;
        const agetSbp = lnAge * trlnsbp;
        const agentSbp = lnAge * ntlnsbp;
        const ageSmoke = patientInfo.relatedFactors.smoker ? lnAge : 0;

        const isAA = patientInfo.relatedFactors.race === 'aa';
        const isMale = patientInfo.gender === 'male';
        let s010Ret = 0;
        let mnxbRet = 0;
        let predictRet = 0;

        const calculateScore = () => {
            if (isAA && !isMale) {
                s010Ret = 0.95334;
                mnxbRet = 86.6081;
                predictRet = (17.1141 * lnAge) + (0.9396 * lnTotalChol) + (-18.9196 * lnHdl) +
                    (4.4748 * ageHdl) + (29.2907 * trlnsbp) + (-6.4321 * agetSbp) + (27.8197 * ntlnsbp) +
                    (-6.0873 * agentSbp) + (0.6908 * Number(patientInfo.relatedFactors.smoker)) +
                    (0.8738 * Number(patientInfo.relatedFactors.diabetic));
            } else if (!isAA && !isMale) {
                s010Ret = 0.96652;
                mnxbRet = -29.1817;
                predictRet = (-29.799 * lnAge) + (4.884 * (lnAge ** 2)) + (13.54 * lnTotalChol) +
                    (-3.114 * ageTotalChol) + (-13.578 * lnHdl) + (3.149 * ageHdl) + (2.019 * trlnsbp) +
                    (1.957 * ntlnsbp) + (7.574 * Number(patientInfo.relatedFactors.smoker)) +
                    (-1.665 * ageSmoke) + (0.661 * Number(patientInfo.relatedFactors.diabetic));
            } else if (isAA && isMale) {
                s010Ret = 0.89536;
                mnxbRet = 19.5425;
                predictRet = (2.469 * lnAge) + (0.302 * lnTotalChol) + (-0.307 * lnHdl) +
                    (1.916 * trlnsbp) + (1.809 * ntlnsbp) + (0.549 *
                        Number(patientInfo.relatedFactors.smoker)) +
                    (0.645 * Number(patientInfo.relatedFactors.diabetic));
            } else {
                s010Ret = 0.91436;
                mnxbRet = 61.1816;
                predictRet = (12.344 * lnAge) + (11.853 * lnTotalChol) + (-2.664 * ageTotalChol) +
                    (-7.99 * lnHdl) + (1.769 * ageHdl) + (1.797 * trlnsbp) + (1.764 * ntlnsbp) +
                    (7.837 * Number(patientInfo.relatedFactors.smoker)) + (-1.795 * ageSmoke) +
                    (0.658 * Number(patientInfo.relatedFactors.diabetic));
            }

            const pct = (1 - (s010Ret ** Math.exp(predictRet - mnxbRet)));
            return Math.round((pct * 100) * 10) / 10;
        };
        return calculateScore();
    }

    computeLifetimeRisk() {
        if (this.patientInfo.age < 20 || this.patientInfo.age > 59) { return null; }
        let ascvdRisk = 0;
        const params = {
            male: {
                major2: 69,
                major1: 50,
                elevated: 46,
                notOptimal: 36,
                allOptimal: 5,
            },
            female: {
                major2: 50,
                major1: 39,
                elevated: 39,
                notOptimal: 27,
                allOptimal: 8,
            },
        };

        const major = (this.patientInfo.totalCholesterol >= 240 ? 1 : 0) +
            ((this.patientInfo.systolicBloodPressure >= 160 ? 1 : 0) +
                (this.patientInfo.relatedFactors.hypertensive ? 1 : 0)) +
            (this.patientInfo.relatedFactors.smoker ? 1 : 0) +
            (this.patientInfo.relatedFactors.diabetic ? 1 : 0);
        const elevated = ((((this.patientInfo.totalCholesterol >= 200 &&
                    this.patientInfo.totalCholesterol < 240) ? 1 : 0) +
                ((this.patientInfo.systolicBloodPressure >= 140 &&
                    this.patientInfo.systolicBloodPressure < 160 &&
                    this.patientInfo.relatedFactors.hypertensive === false) ? 1 : 0)) >= 1 ? 1 : 0) *
            (major === 0 ? 1 : 0);
        const allOptimal = (((this.patientInfo.totalCholesterol < 180 ? 1 : 0) +
                ((this.patientInfo.systolicBloodPressure < 120 ? 1 : 0) *
                    (this.patientInfo.relatedFactors.hypertensive ? 0 : 1))) === 2 ? 1 : 0) *
            (major === 0 ? 1 : 0);
        const notOptimal = ((((this.patientInfo.totalCholesterol >= 180 &&
                    this.patientInfo.totalCholesterol < 200) ? 1 : 0) +
                ((this.patientInfo.systolicBloodPressure >= 120 &&
                    this.patientInfo.systolicBloodPressure < 140 &&
                    this.patientInfo.relatedFactors.hypertensive === false) ? 1 : 0)) *
            (elevated === 0 ? 1 : 0) * (major === 0 ? 1 : 0)) >= 1 ? 1 : 0;

        if (major > 1) { ascvdRisk = params[this.patientInfo.gender].major2; }
        if (major === 1) { ascvdRisk = params[this.patientInfo.gender].major1; }
        if (elevated === 1) { ascvdRisk = params[this.patientInfo.gender].elevated; }
        if (notOptimal === 1) { ascvdRisk = params[this.patientInfo.gender].notOptimal; }
        if (allOptimal === 1) { ascvdRisk = params[this.patientInfo.gender].allOptimal; }

        return ascvdRisk;
    }

    computeLowestTenYear() {
        const patientInfoCopy = Object.assign({}, this.patientInfo);
        patientInfoCopy.systolicBloodPressure = 90;
        patientInfoCopy.totalCholesterol = 130;
        patientInfoCopy.hdl = 100;
        const relatedFactorsCopy = Object.assign({}, this.patientInfo.relatedFactors);
        relatedFactorsCopy.diabetic = false;
        relatedFactorsCopy.smoker = false;
        relatedFactorsCopy.hypertensive = false;
        patientInfoCopy.relatedFactors = relatedFactorsCopy;
        return this.computeTenYearScore(patientInfoCopy);
    };
};

código de ./routes/irc.js

     const Patient = require('../lib/Patient');
    module.exports = function(req, res) {

        const reductions = ['statin', 'sysBP', 'aspirin', 'smoker'];
        const score = 'ten';

        const patient = new Patient(
            req.weight,
            req.height,
            setPatientInfo(
                req.query.gender,
                req.query.age,
                req.query.totCholesterol,
                req.query.hdl,
                req.query.sysBP,
                req.query.smoker,
                req.query.hypertensive,
                req.query.race,
                req.query.diabetic
            )
        );

        res.json({ potencialRisk: patient.computePotentialRisk(reductions, score) });

    };

    const setPatientInfo = (gender, age, totCholesterol, hdl, sysBP, smoker, hypertensive, race, diabetic) => {
        const patientInfo = {};
        const thisDate = new Date();

        patientInfo.gender = gender;
        patientInfo.age = age;

        patientInfo.totalCholesterol = totCholesterol;
        patientInfo.hdl = hdl;
        patientInfo.systolicBloodPressure = sysBP;
        const relatedFactors = {};
        relatedFactors.smoker = smoker;
        relatedFactors.hypertensive = hypertensive;
        relatedFactors.race = race;
        relatedFactors.diabetic = diabetic;
        patientInfo.relatedFactors = relatedFactors;

        return patientInfo;
    };

el código completo está aquí:

https://github.com/MMBBAA/irc-api-tarea5Terminando

11
  • 2
    Al contrario amigo, es bueno que coloques buena parte del codigo, pero a la hora de hacer una pregunta stackoverflow deja bien establecido que no debes poner tu codigo entero, solo los fragmentos necesarios y minimos (ejemplo minimo y verificable) de tu codigo, es decir, si tu fueras yo o algun miembro de la comunidad, serias capaz de revisar y analizar tanto código sin perderte en el??, nosotros no sabemos por completo como esta estructurado tu codigo, y aunque lo supiesemos no sabemos si estamos interpretando el funcionamiento de tu codigo de manera correcta.
    – Layer
    el 12 may. 2020 a las 23:45
  • 2
    Considera editar la pregunta y crear un ejemplo minimo y verificable de donde creas pueda originarse el problema, y no poner de lleno todo el codigo, tambien pues tendrias que poner la información relevante.
    – Layer
    el 12 may. 2020 a las 23:45
  • EL problema que veo es que el método computePotentialRisk no funciona. el 13 may. 2020 a las 0:09
  • 2
    Porfavor toma en cuenta aquello que dije y edita la pregunta y solo pon el código necesario que nos llene de contexto, de lo contrario creo que los usuarios también tardaran un buen rato en darte una respuesta (si es que consiguen darte una respuesta).
    – Layer
    el 13 may. 2020 a las 0:14
  • Editada, el problema que veo es que hay un método que no funciona, básicamente es una fórmula, el problema que tengo es que estoy confuso, hago los console.log de los parámetros que le meto en el get para ver si los pasa y efectivamente los pasa, pero no logro obtener un resultado numérico, solo un null. Dejo el código para que puedas ver la fórmula. el 13 may. 2020 a las 0:28

1 respuesta 1

1

Arreglado, lo único que hacía falta era comprobar hacer un parseint en age en irc.js

patientInfo.age = parseInt(age);

Por otro lado, había que modificar el método computeLifeTimeRisk()

computeLifetimeRisk() {
    if (this.patientInfo.age < 20 || this.patientInfo.age > 59) { return null; }
    var ascvdRisk = 0; //originalmente está como let
    const params = {
        male: {
            major2: 69,
            major1: 50,
            elevated: 46,
            notOptimal: 36,
            allOptimal: 5,
        },
        female: {
            major2: 50,
            major1: 39,
            elevated: 39,
            notOptimal: 27,
            allOptimal: 8,
        },
    };

    const major = (this.patientInfo.totalCholesterol >= 240 ? 1 : 0) +
        ((this.patientInfo.systolicBloodPressure >= 160 ? 1 : 0) +
            (this.patientInfo.relatedFactors.hypertensive ? 1 : 0)) +
        (this.patientInfo.relatedFactors.smoker ? 1 : 0) +
        (this.patientInfo.relatedFactors.diabetic ? 1 : 0);
    console.log("tipo de major: " + typeof(major) + " valor " + major);
    const elevated = ((((this.patientInfo.totalCholesterol >= 200 &&
                this.patientInfo.totalCholesterol < 240) ? 1 : 0) +
            ((this.patientInfo.systolicBloodPressure >= 140 &&
                this.patientInfo.systolicBloodPressure < 160 &&
                this.patientInfo.relatedFactors.hypertensive === false) ? 1 : 0)) >= 1 ? 1 : 0) *
        (major === 0 ? 1 : 0);
    const allOptimal = (((this.patientInfo.totalCholesterol < 180 ? 1 : 0) +
            ((this.patientInfo.systolicBloodPressure < 120 ? 1 : 0) *
                (this.patientInfo.relatedFactors.hypertensive ? 0 : 1))) === 2 ? 1 : 0) *
        (major === 0 ? 1 : 0);
    const notOptimal = ((((this.patientInfo.totalCholesterol >= 180 &&
                this.patientInfo.totalCholesterol < 200) ? 1 : 0) +
            ((this.patientInfo.systolicBloodPressure >= 120 &&
                this.patientInfo.systolicBloodPressure < 140 &&
                this.patientInfo.relatedFactors.hypertensive === false) ? 1 : 0)) *
        (elevated === 0 ? 1 : 0) * (major === 0 ? 1 : 0)) >= 1 ? 1 : 0;

    if (major > 1) {
        let rdo = 0;
        if ('male'.localeCompare(this.patientInfo.gender)) {
            rdo = parseInt(params.male.major2);
            console.log(rdo);
        }

        rdo = parseInt(params.female.major2);

        //ascvdRisk = params[this.patientInfo.gender].major2; //no lee major2
        ascvdRisk = rdo;

    }

    if (major === 1) { ascvdRisk = params[this.patientInfo.gender].major1; }
    if (elevated === 1) { ascvdRisk = params[this.patientInfo.gender].elevated; }
    if (notOptimal === 1) { ascvdRisk = params[this.patientInfo.gender].notOptimal; }
    if (allOptimal === 1) { ascvdRisk = params[this.patientInfo.gender].allOptimal; }

    return ascvdRisk;
}
2
  • 1
    Toma tu +1 buen hombre, mi intención solo era mejorar la pregunta que hiciste, que bien que hayas logrado resolverlo.
    – Layer
    el 14 may. 2020 a las 5:26
  • 1
    gracias por el empujón el 14 may. 2020 a las 5:28

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.