0

I currently have a collection named Consumer_Complaints sitting in a mongoDB database called consumer_Complaints (sorry for making that confusing).

I can access the Consumer_Complaints collection via the mongo shell, but I am unable to access it in my application. I am currently using express.

Here is my code(Updated):

var mongoose = require("mongoose");

//open connection to consumer_Complaints database

mongoose.connect('mongodb://localhost/consumer_Complaints')

//send error if connection fails
mongoose.connection.on('error', console.error.bind(console, 'connection error:' ));

var Schema = mongoose.Schema;

var complaintSchema = new Schema({
    complaint_ID: String,
    product: String,
    sub_Product: String,
    issue: String,
    sub_Issue: String,
    state: String,
    zip_Code: String,
    submitted_Via: String,
    date_Received: String,
    date_sent_to_Company: String,
    company: String,
    company_Response: String,
    timely_Response: String,
    consumer_Disputed: String,
});

complaintSchema.set('collection', 'Consumer_Complaints');

var model = mongoose.model("Model", complaintSchema)

model.find({}, function (err, results) {
    res.json(results);
    console.log(results);//Your Json result
});

Previous Answer- Found Here This seems to be what I want, but the syntax used is different than what I see in the mongoose documents, and when I try to replicate this, I get the following error.

"TypeError: Cannot call method 'find' of undefined"

Any help clarifying the top answer in the linked post, or figuring out what I'm doing wrong would be greatly appreciated!

Community
  • 1
  • 1
user2263572
  • 5,435
  • 5
  • 35
  • 57

2 Answers2

0

Model.find will return a callback. Can you try the following to replace your console.log(Model.find()) .

Model.find({}, function (err, docs) {
  if (err) console.log(err);
  
  console.log(docs);
});

more documentation here: http://mongoosejs.com/docs/api.html#model_Model.find

Ben Wang
  • 36
  • 3
0

//Defined Schema var mongoose = require('mongoose');

var Schema=mongoose.Schema;

var complaintSchema = new Schema({
    complaint_ID: String,
    product: String,
    sub_Product: String,
    issue: String,
    sub_Issue: String,
    state: String,
    zip_Code: String,
    submitted_Via: String,
    date_Received: String,
    date_sent_to_Company: String,
    company: String,
    company_Response: String,
    timely_Response: String,
    consumer_Disputed: String,
    }

complaintSchema .set('collection', 'Consumer_Complaints');

then in server.js:-

app.get('/', function (req, res) {
    console.log("I received a GET request")

    complaintSchema.find({}, function (err, results) {
        res.json(results);
        console.log(results);//Your Json result
    });
});
Prasad
  • 1,562
  • 5
  • 26
  • 40