0

Recently, I changed key of object in MongoDB.

{
 links: {
  changed: 'value',
  notchanged: 'value'
 }
}

This is what I get from my MongoDB collection. Data which key is not changed is still readable by links.notchanged but data which key is changed like links.changed is not readable and only outputs undefined. Node.js gets and reads the whole links data correctly but when it comes to links.changed it doesn't. How do I solve this problem? Code below:

scheme.findOne({}, (err, data) => {
  if (err) res.send('ERR')
  else {
    console.log(data) // prints full data, same as JSON above
    console.log(data.links.changed) // undefined
  }
}
2
  • 1
    if you can send the code, you are using, at least the part that seems to not work. Commented Aug 23, 2021 at 1:25
  • I updated it. It's simple code though. Commented Aug 23, 2021 at 1:52

2 Answers 2

1

You are matching {class:'210'}.. Is it available in document. Probably Your query returns empty object in data . Confirm the match query... Otherwise your code seems ok.

await db1.findOne({class: "210"}, (err, data) => {
        console.log(data.links.changed) // returns value
    })

Or Try the code like this

await db1.find({ class: "210" }).toArray()
        .then(data => {
            console.log(data[0].links.changed) //"value"
        });
Sign up to request clarification or add additional context in comments.

2 Comments

I've modified my question. Can you check it? The problem is not that I can't get data from mongodb but nodejs prints undefined when printing values individually from JSON. class value has no problem as it has class value in full data. I've only uploaded data which is problem.
db1.findOne({}) .then(data => {console.log(data.links.changed)}) .catch(err => console.log(err)); (Try this code)
0

You should make a variable instead of an object for this. For example: Use changed and assign it value as true or false

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.