skip to Main Content

I am trying to simply console.log the output of the mongoose function model.find()
I am using node.js version 18.14.0, "express": "^4.18.2", "mongoose": "^7.0.0",

I have the following issues: model.find no longer accepts callbacks, so I tried to create a variable to console.log the output of model.find():

`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", {useNewUrlParser: true});

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

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

app.get("/articles", function(req, res){
    let docs = Article.find()
    console.log(docs);  
    });`

expected output would be 3 documents with a title and content, that I manually added through 3T, if I use the db.articles.find() command within mongosh, or if I check for the articles through mongoDB Atlas, they show up normally.
I instead receive the following output:

Query { _mongooseOptions: {}, _transforms: [], _hooks: Kareem { _pres: Map(0) {}, _posts: Map(0) {} }, _executionStack: null, mongooseCollection: Collection { collection: null, Promise: [Function: Promise], modelName: 'Article', _closed: false, opts: { autoIndex: true, autoCreate: true, schemaUserProvidedOptions: {}, capped: false, Promise: undefined, '$wasForceClosed': undefined }, name: 'articles', collectionName: 'articles', conn: NativeConnection { base: [Mongoose], collections: [Object], models: [Object], config: {}, replica: false, options: null, otherDbs: [], relatedDbs: {}, states: [Object: null prototype], _readyState: 2, _closeCalled: false, _hasOpened: false, plugins: [], id: 0, _queue: [Array], _listening: false, _connectionOptions: [Object], _connectionString: 'mongodb://localhost:27017/wikiDB', client: [MongoClient], '$initialConnection': [Promise] }, queue: [], buffer: true, emitter: EventEmitter { _events: [Object: null prototype] {}, _eventsCount: 0, _maxListeners: undefined, [Symbol(kCapture)]: false } }, model: Model { Article }, schema: Schema { obj: { title: [Function: String], content: [Function: String] }, paths: { title: [SchemaString], content: [SchemaString], _id: [ObjectId], __v: [SchemaNumber] }, aliases: {}, subpaths: {}, virtuals: { id: [VirtualType] }, singleNestedPaths: {}, nested: {}, inherits: {}, callQueue: [], _indexes: [], methods: {}, methodOptions: {}, statics: {}, tree: { title: [Function: String], content: [Function: String], _id: [Object], __v: [Function: Number], id: [VirtualType] }, query: {}, childSchemas: [], plugins: [ [Object], [Object], [Object], [Object], [Object] ], '$id': 1, mapPaths: [], s: { hooks: [Kareem] }, _userProvidedOptions: {}, options: { typeKey: 'type', id: true, _id: true, validateBeforeSave: true, read: null, shardKey: null, discriminatorKey: '__t', autoIndex: null, minimize: true, optimisticConcurrency: false, versionKey: '__v', capped: false, bufferCommands: true, strictQuery: false, strict: true, pluralization: true }, '$globalPluginsApplied': true }, op: 'find', options: {}, _conditions: {}, _fields: undefined, _updateDoc: undefined, _path: undefined, _distinctDoc: undefined, _collection: NodeCollection { collection: Collection { collection: null, Promise: [Function: Promise], modelName: 'Article', _closed: false, opts: [Object], name: 'articles', collectionName: 'articles', conn: [NativeConnection], queue: [], buffer: true, emitter: [EventEmitter]your text }, collectionName: 'articles' }, _traceFunction: undefined, '$useProjection': true }

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer, the error was in the connection string.

    mongodb://localhost:27017 does not work anymore, you need to use mongodb://127.0.0.1:27017


  2. that’s happen because you were not connected to db yet

    replace mongoose.connect... to be like below

    const connectToDb = async () =>{
     await mongoose.connect("mongodb://localhost:27017/wikiDB", {useNewUrlParser: true});
    }
    connectToDb()
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search