0

I'm running a discord bot and using a mongoDB database to talk to it.

async function checkPoint(arrayPoint, collection){
    const trial = await collection.findOne({ [arrayPoint]: { $exists: true }})
    if(trial == null){
        console.log("Player didn't exist, ergo we make a new area for them, for our good player buddy " + arrayPoint)
        tempObj = "1";
        collection.insertOne({ [arrayPoint]: tempObj}, {arrayPoint} )
    }else if(trial != null){
        console.log("It seems we have data ! Aha !" + arrayPoint )
        console.log(trial.arrayPoint)
        value = parseInt(trial.arrayPoint);
        console.log(value)
        value = value + 1;
        value = value.toString();
        console.log(value)
        collection.updateOne({name: {arrayPoint}}, {$set: {value}})
    }
}

Instead of returning proper values, it returns

undefined NaN NaN

Trial itself is

{
  '96773755035992064': '1'
}

any help would be appreciated I've tried everything in the documentation, and I don't really know what to do?

  • 1
    What "returns"? Nothing of you code actually returns anything. You have a few `console.log` which I assume log `undefined` and 2 times `NaN` ? Do a `console.log(trial)` to see how the `trial` object looks like. It probably doesn't have a `arrayPoint` property (thus the first log of `undefined`). Then you are trying to `parseInt(trial.arrayPoint)` which returns `NaN` (because the parameter is `undefined`) and then you are calculating `NaN + 1` which also gives `NaN` ... – derpirscher Nov 29 '22 at 11:34
  • 1
    What you probably want to do, is if `arrayPoint == '96773755035992064'` you want to access `trial['96773755035992064']`? Then the correct syntax is `value = parseInt(trial[arrayPoint])`; – derpirscher Nov 29 '22 at 11:35
  • Does this answer your question? [How to use a variable for a key in a JavaScript object literal?](https://stackoverflow.com/questions/2274242/how-to-use-a-variable-for-a-key-in-a-javascript-object-literal) – derpirscher Nov 29 '22 at 11:36
  • sorry how do i add more info? – Dougal McSmith Nov 29 '22 at 11:45
  • ` exports.updatePlayerFunctions = (array) =>{ arrayLength = array.length - 1; const colecter = userdb.collection("Points"); for(let i = 0; i < array.length; i++){ checkPoint(array[i], colecter) } } ` this code is used to enter a string which IS a field name in a mongodb log. Trial isn't an array, its a mongodb datapoint., – Dougal McSmith Nov 29 '22 at 11:46
  • Your current code is literally accessing a property named `arrayPoint`, ie `{ arrayPoint: "1"}` Does your mongodb document have a field named `arrayPoint`? Judging from your output, it does not ... And also juding from the way you insert it as `{[arrayPoint]: ...}` the parameter arrayPoint you are passing to your `checkPoint` function carries the name of the field, you want to access. So accessing `trial.arrayPoint` is wrong, and `trial[arrayPoint]` is the way to go ... – derpirscher Nov 29 '22 at 12:14

0 Answers0