Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
how to pass var inside the json post showing error that is not a number
//function for getting old xp of user
var oldxpuser;

 function old_Xp(UID){

  return new Promise(function (resolve, reject) {

    redmine.users(UID,function(err,data) {
      if (!err){
      for (var i=0; i < data.users.length; i++) {
        if(data.users[i].id == UID){
            oldxpuser = parseInt(data.users[i].custom_fields[0].value);
            console.log("old xp: "+ oldxpuser);
        }
      }
      resolve(oldxpuser);
    }
      else{
        reject(err);
      
     
    }

    })
  })
}

//after old xp get then update will run

old_Xp().then(function () {
  update_Xp(8)
}).catch(function(error) {
    console.log("Error occured ERROR:" + error);
})

old_Xp(8);




//////////////////////////////////////////////////////////

  function update_Xp(UID){

    redmine.update_user(UID, updated_XP ,function(err, data) {
      if (err) 
        console.log(err);
  
      console.log('Updated !!');
    });
  
  console.log("ME: your promise has been resolved");
  }
  //////////////////////////////////////////////////////////
// console.log(oldxpuser);


let new_xp = oldxpuser+50;
console.log( parseInt(new_xp));
  let updated_XP=
  {
    user: {
      custom_fields: [{id: 1    ,value: new_xp}],     //id is important
    }
  }

//////////////////////////////////////////////////////////

// update_Xp(8);     //accepts user ID
check_XP();

in this above code i mainly created 2 function old_xp and update_xp so i want that first old_xp will be run so i can get the old value from user and then update_xp will run but it not working like that var oldxpuser is showing error of NaN or ( it just running the update_xp first to cant able to get oldxpuser) enter image description here

about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

I see a few possible issues here.

First. When you call old_XP() first time you do not pass a uid, that doesn't allow you setup oldxpuser later, as id is undefined.

So, the fix is to add uid 8 there as well.

old_Xp(8).then(function () {
  update_Xp(8)
})

Second. update_Xp uses new_xp which is a synchronous variable, but that synchronous variable is dependent on the async oldxpuser, that means at the moment when you create new_xp variable, the oldxpuser is still undefined (it will be set up only when promise old_XP is resolved).

The fix here is to generate updated_XP object dynamically, like here:

redmine.update_user(UID, getUpdatedXp() ,function(err, data) {});

function getUpdatedXp() {
  const new_xp = oldxpuser + 50;
  console.log("updatedXP: " + new_xp);

  return {
    user: {
      custom_fields: [{ id: 1, value: new_xp }],
    }
  }
}
//////////////////////////////////////////////////////////

Example:

//function for getting old xp of user
var oldxpuser;
var usersData = [
  { id: 8, custom_fields: [{ value: 8 }] },
  { id: 1, custom_fields: [{ value: 1 }] },
];
var redmine = {
  users: (uid, callback) => {
    const users = usersData.filter(user => user.id === uid);
    setTimeout(() => callback(null, { users }), 100);
  },
  update_user: (uid, newUsersData, callback) => {
    const userData = usersData.find(user => user.id === uid);
    userData.custom_fields[0].value = newUsersData.user.custom_fields[0].value;
    setTimeout(() => callback(null, { userData }), 100);
  },
};

 function old_Xp(UID){

  return new Promise(function (resolve, reject) {

    redmine.users(UID,function(err,data) {
      if (!err){
      for (var i=0; i < data.users.length; i++) {
        if(data.users[i].id == UID){
            oldxpuser = parseInt(data.users[i].custom_fields[0].value);
            console.log("old xp: "+ oldxpuser);
        }
      }
      resolve(oldxpuser);
    }
      else{
        reject(err);
      
     
    }

    })
  })
}

//after old xp get then update will run

old_Xp(8).then(function () {
  update_Xp(8)
}).catch(function(error) {
  console.log("Error occured ERROR:" + error);
});
old_Xp(8);

//////////////////////////////////////////////////////////

  function update_Xp(UID){

    redmine.update_user(UID, getUpdatedXp() ,function(err, data) {
      if (err) 
        console.log(err);
  
      console.log('Updated !!');
    });
  
  console.log("ME: your promise has been resolved");
  }
  //////////////////////////////////////////////////////////
// console.log(oldxpuser);


let new_xp = oldxpuser+50;
console.log( parseInt(new_xp));
  let updated_XP=
  {
    user: {
      custom_fields: [{id: 1    ,value: new_xp}],     //id is important
    }
  }

function getUpdatedXp() {
  const new_xp = oldxpuser + 50;
  console.log("updatedXP: " + new_xp);

  return {
    user: {
      custom_fields: [{ id: 1, value: new_xp }],
    }
  }
}
//////////////////////////////////////////////////////////

about 4 years ago · Santiago Gelvez Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda