Issue Tracker Project

Hello there! I’m working on this project by freeCodeCamp and I’m trying to assign a value from a collection in a database to a variable. The JSON object in the data has a field called issue_title. I am trying to assign that value to a variable if an input field is empty.

The code snippet is as shown below:

let issueTitle = req.body.issue_title !== ‘’? req.body.issue_title : user.findOne({_id:_id}, (err, data) => {
if(err) console.log(err);
return data.issue_title
});

When I console.log the data.issue_title, I get the value that I’m looking for. However, when I assign the value to the variable ‘issueTitle’ and I console.log the variable, I get a very weird output in console.

Query {

_mongooseOptions: {},
_transforms: [],
mongooseCollection:
NativeCollection {
collection: Collection { s: [Object] },
… it goes on for quite a few lines

Am I doing something wrong? Help would be greatly appreciated :slight_smile:

Anyone? :rofl::rofl::joy:

I’m working on this part of the project right now. I think I am having a similar problem as you were. How to either not update issue_title if there is no input, or update issue_title using the current value in the database.

I used findById to retrieve data which console.log(doc.issue_title); //correctly logs "new issue" because that is what is in the db.

But when I try to use doc.issue_title with findAndUpdate to update the file I get a message on the app page there is an error, but there is no errors in the console or the console on glitch where my project is.

Do you remember how you solved this? Any hints or tips?

Hi. Sorry for the late reply.

Try checking out this : https://glitch.com/~issue-tracker-edwin-fcc

I remember solving it, just don’t remember how. Hope this helps :slight_smile:

Thank you! I’ll take a look at it

@Edwinyms and @eoja
Regarding the original post, using async/await might help here.
For example, if I keep a collection called “issues” and each Issue is made from a schema like this,

{
  title: String,
  username: String,
  message: String
}

I could do the following:

app.get("/issues/:id", async (req, res) => {
    const foundIssue = await Issue.findById(req.params.id)
    if (!foundIssue) { console.log("Sorry, no issue found") }
    if (foundIssue) { console.log(issue.title) }
})