troubleshooting Question

How do I pass this variable into my method?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
JavaScriptNode.js
5 Comments1 Solution138 ViewsLast Modified:
Here is my method:

 this.displayActivityForm = function (companyId, proposalId, type, activityId) {
            console.log(activityId);
            $.get('/companies/activities/' +activityId, function(resp) {
                      if (resp.error) {
                    CAMS.alert('Error', resp.msg, 'error');
                    return false;
                }
                  let type = resp.type;
                  let notes = resp.notes;
                  let date = resp.meta.date;
                  console.log(date);
            });

I can successfully do the "console.log(activityId) that you see on the second line, but there's something about the way I'm passing that value into my route that's coming up short.

First of all, here's the route:

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

...and here's the Activity service:

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




Here's the error I'm getting (BTW: there's a lot here, but I have in bold what I believe the problem to be):

Error:  Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (C:\wamp64\www\bSmart\node_modules\mongodb\node_modules\bson\lib\bson\objectid.js:59:11)
    at Activity._getId (C:\wamp64\www\bSmart\server\services\service.js:661:24)
    at C:\wamp64\www\bSmart\server\services\activity.js:27:36
    at new Promise (<anonymous>)
   at Activity.getOne (C:\wamp64\www\bSmart\server\services\activity.js:26:16)
    at C:\wamp64\www\bSmart\server\routes\companies.js:833:33

    at Layer.handle [as handle_request] (C:\wamp64\www\bSmart\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\wamp64\www\bSmart\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\wamp64\www\bSmart\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\wamp64\www\bSmart\node_modules\express\lib\router\layer.js:95:5)
    at C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:281:22
    at param (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:354:14)
    at param (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:410:3)
    at next (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:174:3)
    at router (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\wamp64\www\bSmart\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:317:13)
    at C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:335:12)
    at next (C:\wamp64\www\bSmart\node_modules\express\lib\router\index.js:275:10)
false
MongooseError [CastError]: Cast to ObjectId failed for value "false" at path "_id" for model "Activity"
    at new CastError (C:\wamp64\www\bSmart\node_modules\mongoose\lib\error\cast.js:29:11)
    at ObjectId.cast (C:\wamp64\www\bSmart\node_modules\mongoose\lib\schema\objectid.js:244:11)
    at ObjectId.SchemaType.applySetters (C:\wamp64\www\bSmart\node_modules\mongoose\lib\schematype.js:948:12)
    at ObjectId.SchemaType._castForQuery (C:\wamp64\www\bSmart\node_modules\mongoose\lib\schematype.js:1362:15)
    at ObjectId.SchemaType.castForQuery (C:\wamp64\www\bSmart\node_modules\mongoose\lib\schematype.js:1352:15)
    at ObjectId.SchemaType.castForQueryWrapper (C:\wamp64\www\bSmart\node_modules\mongoose\lib\schematype.js:1331:15)
    at cast (C:\wamp64\www\bSmart\node_modules\mongoose\lib\cast.js:307:32)
    at model.Query.Query.cast (C:\wamp64\www\bSmart\node_modules\mongoose\lib\query.js:4608:12)
    at model.Query.Query._castConditions (C:\wamp64\www\bSmart\node_modules\mongoose\lib\query.js:1790:10)
    at model.Query.<anonymous> (C:\wamp64\www\bSmart\node_modules\mongoose\lib\query.js:2045:8)
    at model.Query._wrappedThunk [as _findOne] (C:\wamp64\www\bSmart\node_modules\mongoose\lib\helpers\query\wrapThunk.js:16:8)
    at C:\wamp64\www\bSmart\node_modules\mongoose\node_modules\kareem\index.js:369:33
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  message: 'Cast to ObjectId failed for value "false" at path "_id" for model "Activity"',
  name: 'CastError',
  stringValue: '"false"',
  kind: 'ObjectId',
  value: false,
  path: '_id',
  reason: undefined,
  model: Model { Activity }
}
(node:18160) UnhandledPromiseRejectionWarning: ReferenceError: error is not defined
    at C:\wamp64\www\bSmart\server\routes\companies.js:836:13
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
(node:18160) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18160) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

It would appear that although I have a credible, incoming "activityId" coming into the method, the way in which I'm passing it into my route / method is coming up short.

What am I doing wrong?

BTW: When I run this as my URL: companies/activities/5e8492c99c41674ce023f7f3

I get a good result.

So, the route and the service are sound, it's just the way I'm passing that activityId into the mix from my initial method that's bogus, correct?

What am I missing?
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 5 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 5 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