Piyawat
Piyawat

Reputation: 23

MongoDB data return []

I'm making a project for my college assignment, but I'm having a trouble while i'm trying to get a data from database called 'project' in 'sitelist' collection from MongoDB. But for some reason the data i got is only an empty array [ ].

I'm new to MongoDB so i want to know where did i do wrong.

server.js

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const app = express();
const SLDBModel = require('./sldb_schema');         
require('./db');

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

app.use(function (req, res, next)
{
    res.setHeader('Access-Control-Allow-Origin','*');
    res.setHeader('Access-Control-Allow-Methods','GET, POST, PUT, DELETE');
    res.setHeader('Access-Control-Allow-Headers','content-type, x-access-token');
    res.setHeader('Access-Control-Allow-Credentials', true);
    next();
});

app.get('/api/getList', function (req, res){   
    SLDBModel.find({},function(err, data){
        if(err){
            console.log('//app.get// getList error!...');
            res.send(err);
        }
        else{   
            console.log(data);
            res.send(data.map(v => v.toJSON()));
        }
    });
});

module.exports = app;

app.listen(3000, ()=>{
    console.log("SERVER IS ONLINE! ON PORT 3000");
})

sldb_schema.js

var mongoose = require('mongoose');
const Schema = mongoose.Schema;

const SLSchema = new Schema({
        S_NAME: {type: String, required: true},
        S_ADD: {type: String, required: true},
        S_STATUS: {type: String, required: true}
    }
);

const SLDBSchema = new Schema({
        list: [SLSchema]
    }
);

const SLDBModel = mongoose.model('sitelist', SLDBSchema);
module.exports = SLDBModel;

db.js

var mongoose = require('mongoose');

mongoose.connect('mongodb://127.0.0.1/project', {useUnifiedTopology: true, useNewUrlParser: true });

mongoose.connection.on('connected', function () {
    console.log('MONGOOSE default connection open');
});
mongoose.connection.on('error', function (err) {
    console.log('MONGOOSE default connection error: ' + err);
});
mongoose.connection.on('disconnected', function () {
    console.log('MONGOOSE default connection disconnected');
});
process.on('SIGINT', function () {
    mongoose.connection.close(function () 
    {
        console.log('MONGOOSE default connection disconected through app termination');
        process.exit(0);
    });
});

Data in database called 'project' in collection 'sitelist'

{
    "_id" : ObjectId("5ec4672e44f01dcae82c3dde"),
    "error" : "0",
    "num_rows" : 3,
    "list" : [ 
        {
            "S_NAME" : "SBOBETH",
            "S_ADD" : "sbobeth.com",
            "S_STATUS" : "UP"
        }, 
        {
            "S_NAME" : "GTRCASINO",
            "S_ADD" : "gtrcasino.com",
            "S_STATUS" : "DOWN"
        }, 
        {
            "S_NAME" : "SBOBETH",
            "S_ADD" : "sbobeth.com",
            "S_STATUS" : "DOWN"
        }, 
        {
            "S_NAME" : "GTRBETCLUB",
            "S_ADD" : "gtrbetclub.com",
            "S_STATUS" : "UP"
        }, 
        {
            "S_NAME" : "77UP",
            "S_ADD" : "77up.bet.com",
            "S_STATUS" : "UP"
        }, 
        {
            "S_NAME" : "DATABET88",
            "S_ADD" : "databet88.net",
            "S_STATUS" : "DOWN"
        }, 
        {
            "S_NAME" : "FAFA855",
            "S_ADD" : "fafa855.com",
            "S_STATUS" : "UP"
        }
    ]
}

Upvotes: 0

Views: 60

Answers (1)

Nont Kung
Nont Kung

Reputation: 26

Because your collection's name is not pluralize. Please check answer in below link.

MongoDB and Mongoose: Cannot retrieve data

Upvotes: 1

Related Questions