troubleshooting Question

Why does this query not work?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
JavaScriptNode.js
2 Comments1 Solution137 ViewsLast Modified:
Here's my URL:

http://localhost:3000/companies/activities/5e8492c99c41674ce023f7f3

Here's my route:

router.get('/companies/activities/:activityId', async(req, res)=> {
      try {
            //console.log(req.params.activityId);
            let activityId=req.params.activityId;
        await Activity.getOne(activityId);
        res.send(Company.success());
    } catch (err) {
        res.send(Proposal.error('An unexpected error was encountered while trying to load your proposal. Please try again.'));
    }
});

Here's my service:

const { ObjectId } = require('mongodb');
const { Util } = require('node-utils');
const moment = require('moment-timezone');
const Service = require('./service');

const mongoose = require('mongoose');
const ProposalSchema = require('../schemas/ProposalSchema');
const CompanySchema = require('../schemas/CompanySchema');
const UserSchema = require('../schemas/UserSchema');
const ActivitySchema = require('../schemas/ActivitySchema');

class Activity extends Service {

    constructor() {
        super();

        this.collection = 'activities';
        this.plural = 'activities';
        this.singular = 'activity';
        this.model = mongoose.model('Activity', ActivitySchema);        
        //this.fields = require('../lib/model-proposal');
    }   
	
	   getOne(activityId) {
		   //console.log(activityId);
        return new Promise((resolve, reject) => {
            let activity_Id = this._getId(activityId);
			console.log(activity_Id);
            this.model.findOne({ _id: activity_Id }, (err, res) => {
                if (err) { console.log(err); reject(err); }
                resolve(res);
            });
        });
    }
}

module.exports = new Activity();

The _getId method is in the "service.js" service and that's this guy:


    _getId(id) {
        try {
            if (typeof id == 'object') {// && id instanceof ObjectID) {
                return id;
            } else if (typeof id == 'string') {
                return new ObjectID(id);
            } else {
                return false;
            }
        } catch (err) {
            console.log('Error: ', err);
            return false;
        }
    }

In my collection, I have this:

collection
The table seems to be set, yet I'm getting this error:

{"error":false,"msg":""}

So, my "getOne" query is failing, but I don't know why.

Do you see anything that I'm doing wrong? How do I go about troubleshooting this thing?

Thanks!

I've been able to console out my id as well as all of the obvious elements...
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 2 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros