Avatar of Bruce Gust
Bruce GustFlag for United States of America

asked on 

Why does this query not work?

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();

Open in new window


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:

User generated image
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...
Node.jsJavaScript

Avatar of undefined
Last Comment
Bruce Gust
ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Bruce Gust
Bruce Gust
Flag of United States of America image

ASKER

That worked, Chris! I'm now getting some valid data!

Here's my next beast: https://www.experts-exchange.com/questions/29178499/How-do-I-passing-this-variable-into-my-method.html#questionAdd 
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo