0

I am uploading video details such as title, thumbnail, video file, and category to DB and displaying all thumbnails on the home page and while clicking on the thumbnail it will be forwarded to another page(watch video).

The home page is working fine with thumbnail, but when I click on the thumbnail it forwards to watch video page but it doesn't load the video path correctly but the title is displaying correctly. I am getting the following error while loading watch-video page. Help me to find the issue.

Error

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (D:\Bitady\hakeek\node_modules\bson\lib\bson\objectid.js:59:11)
    at ObjectID (D:\Bitady\hakeek\node_modules\bson\lib\bson\objectid.js:40:43)
    at exports.getwatchVideo (D:\Bitady\hakeek\controllers\user.js:32:20)
    at Layer.handle [as handle_request] (D:\Bitady\hakeek\node_modules\express\lib\router\layer.js:95:5)
    at next (D:\Bitady\hakeek\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (D:\Bitady\hakeek\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (D:\Bitady\hakeek\node_modules\express\lib\router\layer.js:95:5)
    at D:\Bitady\hakeek\node_modules\express\lib\router\index.js:281:22
    at param (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:354:14)
    at param (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:365:14)
    at Function.process_params (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:410:3)
    at next (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:174:3)
    at router (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (D:\Bitady\hakeek\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (D:\Bitady\hakeek\node_modules\express\lib\router\index.js:317:13)

Routes in main.js

app.use('/admin', adminRoutes);
app.use(userRoutes);
app.use(authRoutes);

Routes - user.js

   router.get('/about', userController.getAbout);
   router.get('/watch-video/:videoId', userController.getwatchVideo);
   router.get('/', userController.getVideos);

watch-video - Controller

exports.getwatchVideo = (req, res, next) => {
    const id =  req.params.videoId;
   const videoID = ObjectId(req.params.videoId);
   //console.log(videoID);
   //console.log(id);
   //const videoID = req.params.videoId;
    //Video.findOne({_id: videoID})
    Video.findById(id)
    .then(videoFile => {
        if(!videoFile){
            return console.log('Video not found');            
        }
        console.log(videoFile);
        res.render('user/watch', {
            wvideoData: videoFile,
            pageTitle: videoFile.title,
            path: 'watch-video'            
        });
    })
    .catch(err => {
        console.log(err);
    });

};

MRZ
  • 23
  • 1
  • 6
  • are you sure that your `id` is valid objectid? – bill.gates Jun 24 '20 at 09:02
  • You passed string `id` inside this `Video.findById(id)`, can you pass you have created const variable with objec tid in `videoID` variable, pass this to `Video.findById(videoID)`. – turivishal Jun 24 '20 at 09:19
  • @turivishal It is showing same error – MRZ Jun 24 '20 at 09:23
  • @Ifaruki Yes, I am passing <%= video._id %> from home page. – MRZ Jun 24 '20 at 09:24
  • i was not asking if you pass it, i asked is this ID valid? This error happens if you try to convert a string that is not a valid mongo objectid – bill.gates Jun 24 '20 at 09:26
  • Does this answer your question? [Argument passed in must be a string of 24 hex characters - I think it is](https://stackoverflow.com/questions/30051236/argument-passed-in-must-be-a-string-of-24-hex-characters-i-think-it-is) – Fathma Siddique Jun 24 '20 at 09:28
  • @Ifaruki It is valid objectID let me check how to check if an Object is valid or not – MRZ Jun 24 '20 at 09:29
  • could you show us the id? – bill.gates Jun 24 '20 at 09:30
  • @Ifaruki 5ef31d8e18db1a31700b61bf This is the id i console.logged – MRZ Jun 24 '20 at 09:41
  • @FathmaSiddique No, I tried that. – MRZ Jun 24 '20 at 09:43
  • @FathmaSiddique When I did like that it is showing the following error "CastError: Cast to ObjectId failed for value "main.js" at path "_id" for model "Video" " – MRZ Jun 24 '20 at 09:55
  • @MirswadKarayan could you please try `console.log(mongoose.Types.ObjectId.isValid(req.params.videoId))` and tell me the result. Add `const mongoose = require('mongoose');` too – Fathma Siddique Jun 24 '20 at 10:10
  • @FathmaSiddique the result is "false" , could you please what is the issue here – MRZ Jun 24 '20 at 10:22
  • For some reasons, what you are passing in the param as `videoId` in the route `router.get('/watch-video/:videoId', userController.getwatchVideo);` is not a valid ObjectID. You should check the previous controller or view from where you are setting value for `videoId`..make sure you are passing a valid ObjectID. – Fathma Siddique Jun 24 '20 at 10:29
  • @FathmaSiddique in the previous controller i am fetching all data with find() method and passed it to the view – MRZ Jun 24 '20 at 10:46
  • could you please provide me the view section for that route? – Fathma Siddique Jun 24 '20 at 10:53
  • @FathmaSiddique <%- include('../includes/header') %> <%- include('../includes/navigation') %>

    User Home page

    Recent Uploads

    <% for (let items of videos) { %>

    <%= items.title %>

    <% } %> <%- include('../includes/footer') %>
    – MRZ Jun 24 '20 at 11:03
  • @FathmaSiddique <% for (let items of videos) { %>

    <%= items.title %>

    <% } %>
    – MRZ Jun 24 '20 at 11:04
  • @MirswadKarayan I don't see any issue here! – Fathma Siddique Jun 24 '20 at 11:16
  • @FathmaSiddique so what will be the problem, shall I share my screen? – MRZ Jun 24 '20 at 12:01
  • @MirswadKarayan what is the version of mongoose that you are using? check the link https://stackoverflow.com/questions/41567319/getting-error-in-mongodb-cast-issue-for-valid-object-id – Fathma Siddique Jun 24 '20 at 13:01
  • @FathmaSiddique 5.9.18 – MRZ Jun 24 '20 at 16:26
  • @FathmaSiddique When I rechecked the code , i found that i had an error previously with "MongoError: no primary found in replicaset or invalid replica set name" . and i solved that by adding "{ useUnifiedTopology: true } " along with connection string. Now i removed it and trying to solve the MongoError, I hope once It is solved the ID problem can be solved. – MRZ Jun 25 '20 at 06:02

0 Answers0