Quality Assurance Projects - Issue Tracker

Tell us what’s happening:

Describe your issue in detail here.
Good day, so I have just done a post request on replit and when I try it out on my live website it doest appear at all, I tried creating a post and no json respose at all

Your project link(s)

‘use strict’;

const mongoose = require(‘mongoose’);
const IssueModel = require(“…/models”).Issue;
const ProjectModel = require(“…/models”).Project;

module.exports = function (app) {

app.route(‘/api/issues/:project’)

.get(function (req, res){
  let project = req.params.project;
  
})

.post(function (req, res){
  let project = req.params.project;
  const {
    issue_title,
    issue_text,
    created_by,
    assigned_to,
    status_text,
  } = req.body;
  if (!issue_title || !issue_text || !created_by) {
    res.json({ error: "required field(s) missing"});
    return;
  }
  const newIssue = new IssueModel({
    issue_title: issue_title || "",
    issue_text: issue_text || "",
    created_on: new Date(),
    updated_on: new Date(),
    created_by: created_by || "",
    assigned_to: assigned_to || "",
    open: true,
    status_text: status_text || "",
  });
  ProjectModel.findOne( {name: project}, (err, data) => {
    if (!projectData) {
      const newProject = new ProjectModel({ name: project });
      newProject.issues.push(newIssue);
      newProject.save((err, data) => {
        if (err || !data) {
          res.send("There was an error saving in post");
        } else {
          res.json(newIssue);
        }
      });
    } else {
      projectData.issues.push(newIssue);
      projectData.save((err, data) => {
        if (err || !data) {
          res.send("There was an error saving in post");
        } else {
          res.json(newIssue);
        }
      });
    }
  });
  
})

.put(function (req, res){
  let project = req.params.project;

  
})

.delete(function (req, res){
  let project = req.params.project;
  
});

};

solution: boilerplate-project-issuetracker (2) - Replit

Your browser information:

am using chrome
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36

Challenge Information:

Quality Assurance Projects - Issue Tracker

Are you not getting an error?

TypeError: IssueModel is not a constructor

models isn’t a folder, it is a file.

After you fix that you should get another error.

MongooseError: Model.findOne() no longer accepts a callback

The version of Mongoose you are using does not use callbacks anymore. Refactor the code or downgrade Mongoose (V6 or below as far as I remember).


As for your question, I think you also have to make sure you use the New tab to test the form and not the embedded view.

Good day thanks, I want to ask how can I go about refactoring the code cause i have no idea how to do it

Change the version in the package.json and let it update the dependencies.

Or use the Shell tab (next to the Console tab).

npm rm mongoose
npm i mongoose@6.12.3

I might suggest you take the time to learn it using async/await

https://mongoosejs.com/docs/7.x/docs/migrating_to_7.html#dropped-callback-support

Okay Ill try it out, Thank you

Good day, so I tried it this way:
.post(async (req, res) => {
let project = req.params.project;
const { issue_title, issue_text, created_by, assigned_to, status_text } = req.body;
if (!issue_title || !issue_text || !created_by) {
res.json({ error: “required field(s) missing” });
return;
}
try{
let projectModel = await ProjectModel.findOne({ name: project });
if (!projectModel) {
projectModel = new ProjectModel({ name: project });
await projectModel.save();
}
const issueModel = new IssueModel({
project_Id: projectModel.id,
issue_title: issue_title || “”,
issue_text: issue_text || “”,
created_on: new Date(),
updated_on: new Date(),
created_by: created_by || “”,
open: true,
assigned_to: assigned_to || “”,
status_text: status_text || “”,
});
const issue = await issueModel.save();
res.json(issue);
} catch (err) {
res.json({ error: ‘Error saving issue’ });
}
})

and when I check it on the live app, I only get this message:
{“error”:“Error saving issue”}
{“error”:“Error saving issue”}

  • You are not connecting to the DB.

  • You are not exporting/requiring the models correctly.

  • You are not using the correct property name for the IssueModel model id (projectId not project_Id).

  • You have projectData_id in the code, _id is a property on the object (you are missing a dot).


Never try/catch without logging or sending the actual error (the catch param). If something throws you want to know why.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.