Gilbert21
Gilbert21

Reputation: 1

Empty array problem!!!!!! using node.js, mongoose connected to mongodb database

I tried all the solving on the internet and still got an empty array, I use Studio 3T connected it to MongoDB database and I want to use app.get to see my collection's object. I used the Studio 3T terminal and used db.articles.find() where I can see the objects. But on hyper terminal no, I can't get anything and the code is exactly as from most of the people in the course, as I checked everything for the spelling of letters if it's capital or not but all alright nothing seems wrong, so I hope anyone helps me with that. Thank you in advance!!! This is the code below:


const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const ejs = require("ejs");

const app = express();

app.set("view engine", "ejs");

app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

mongoose.connect("mongodb://localhost:27017/wikiDB");

const articleSchema = {
  title: String,
  content: String
};

const Article = mongoose.model("Article", articleSchema);

app.get("/articles", function(req, res){

  Article.find({}, function(err, foundArticles){
    res.send(foundArticles);

  });
});

app.listen(3000, function() {
  console.log("Server started on port 3000");
});

And here the terminal of Studio 3T(Robo 3T) looks like this and I can see the objects of my collection: enter image description here

Upvotes: 0

Views: 104

Answers (1)

Rıdvan Can
Rıdvan Can

Reputation: 89

It will work if you make changes like below.

  const articleSchema = new mongoose.Schema({
      title: String,
      content: String
  });

Upvotes: 0

Related Questions