0

I am trying to display multiple documents from mongodb on my frontend html using node.js and express. it basically gets document from my mongodb and then it sends it to the ejs file and the loop executes the code, but i dont know what i am doing wrong here. please help... here is my code snippet...

var jobSchema = new mongoose.Schema({
    Company: String,
    Job_Title: String,
    Job_Description: String,
    Payment: String,
    Phone: String,
    Email: String
})

var Job = mongoose.model('Job', jobSchema, 'Jobs')

app.post("/seejobs", function (req, res) {
    
    Job.find().toArray(function(err, docs) {
        if(err) throw err;
        res.render('main', {'docs': docs});
    });
    
});

and my ejs looks like...

<% for (var d=0 ; d < docs.length ; d++){%>

                <h1>Company : <%= docs[d].Company %></h1>
                <h1>Job Title : <%= docs[d].Job_Title %></h1>
                <h1>Job Description : <%= docs[d].Job_Description %></h1>
                <h1>Payment : <%= docs[d].Payment %></h1>
                <h1>Phone : <%= docs[d].Phone %></h1>
                <h1>Email : <%= docs[d].Email %></h1>

            <% } %>

and here is the error i get...

TypeError: Job.find(...).toArray is not a function
    at C:\Users\Xander CaGe\Desktop\Projects\Joby\app.js:53:16
    at Layer.handle [as handle_request] (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\express\lib\router\index.js:275:10)
    at C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\body-parser\lib\read.js:130:5
    at invokeCallback (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\raw-body\index.js:224:16)
    at done (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\raw-body\index.js:213:7)
    at IncomingMessage.onEnd (C:\Users\Xander CaGe\Desktop\Projects\Joby\node_modules\raw-body\index.js:273:7)
    at IncomingMessage.emit (events.js:333:22)
    at endReadableNT (_stream_readable.js:1201:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

Can anyone help with my code?

4
  • i dont know what i am doing wrong We don't know either :) What's the output? What do you see happening? Any error? Crash? Are you getting a blank page? Anything? All we can suppose is that "something" doesn't happen as expected, but what? As far as I can tell, this code doesn't look bad Commented Sep 17, 2020 at 14:30
  • i have added the error i get. Commented Sep 17, 2020 at 15:09
  • i dont understand what i did wrong here... Commented Sep 17, 2020 at 15:12
  • Aaaah, see? With the error, somebody found right away. Now it's obvious. Commented Sep 18, 2020 at 6:33

3 Answers 3

2

...toArray is not a function

Regarding the error you are seeing. toArray is not part of the Mongoose API. Hence Job.find().toArray won't work. Try this :


Job.find({}).exec()
.then((docs) => {
  // note 'docs' is already an Array of Documents
  res.render('main', {'docs': docs});
})
.catch((err) => {
  if(err) throw err;
});

See : https://mongoosejs.com/docs/api.html#model_Model.find

Sign up to request clarification or add additional context in comments.

Comments

2

To elaborate on @PascalLamers's answer, here's the ES6/async/await style. Add .lean() to get only plain JSON data instead of Mongoose objects, which is lighter and faster :

app.post("/seejobs", async (req, res) => {
    try{
        const docs = await Job.find({}).exec().lean();
        res.render('main', { 'docs': docs });
    } catch(err) {
        res.json(err);
    }
});

Comments

0

This should do the job for you.

    Job.find().then(function(err, docs) {
      if(err) throw err;
      res.render('main', {'docs': docs});
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.