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

177
Visualizações
Error:Cast to Number failed for value "undefined58" (type string) at path "TimePeriod"

Trying to add taxes to a user using 3 parameters of User, original, and percent. The schemas are defined below as well as the calling of the function

const usertaxschema = require('./schemas/usertaxschema')
const maintaxschema = require('./schemas/maintaxschema')
  const taxedamount = ((percent/100)*original).toFixed(0)
  const data = await usertaxschema.findOne({User:User})
  if(!data) {
    const newSchema = new usertaxschema({
      User:User,
      TaxesPaidInTotal:0,
      TakesPaidThisPeriod:0,
      })
      await newSchema.save().catch((err) => console.log(err))
      return 0
  }
  const chubster = await maintaxschema.findOne({Taxes:'Main'})
  chubster.TotalTaxesPaid +=taxedamount
chubster.taxesavailable += taxedamount
data.TotalTaxesPaid +=taxedamount
data.TimePeriod += taxedamount
await chubster.save()
await data.save()
return taxedamount
}

UserTaxSchema

const mongoose = require('mongoose')
const usertaxschema = mongoose.Schema({
    User:String,
    TaxesPaidInTotal:Number,
    TimePeriod:Number,
    })
module.exports = mongoose.model('usertaxes', usertaxschema, 'usertaxes')

MainTaxSchema

const mongoose = require('mongoose')
const maintaxschema = mongoose.Schema({
    Taxes:String, //Main etc
    TotalTaxesAllTime:Number,
    TaxesCollectedThisPeriod:Number,
    TaxesInBalance:Number,
})
module.exports = mongoose.model('maintaxes', maintaxschema, 'maintaxes')

Example of calling function

 const fatso = await Client.economy.taxUser(interaction.user.id, 500, 11.5)
    console.log(fatso)
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

Number.toFixed returns a String. That's where "58" comes from. Then you "add" (concatenate) the String "58" to various properties that should be storing numbers, but it seems that the property TimePeriod is undefined, so then it concatenates undefined and "58" and you get the value "undefined58".

You have to fix two things:

  1. make sure to add numbers to numbers, instead of adding Strings to numbers
  2. check if the properties are undefined before you add to them

To solve #1, this line

const taxedamount = ((percent/100)*original).toFixed(0)

should use Math.round instead of .toFixed, because Math.round returns a number, while .toFixed returns a String.

const taxedamount = Math.round(percent / 100 * original)

To solve #2, you need to check for undefined (maybe even for null, if that's also a possible value) before you add values. But are you sure you should be adding a "taxed amount" to a "time period"?

Well anyway, if that is what you want to do, and if you want to add the amount even if the current value of the property is undefined (or null) you can default to 0 before you add

data.TimePeriod = (data.TimePeriod || 0) + taxedamount

You should do that for all properties that can contain undefined (or null) in order to not get a NaN (not a number) value.

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