Как получить данные из mongodb в nodejs и angular - PullRequest
0 голосов
/ 08 мая 2020

Я пытаюсь получить данные от mongodb для отображения в таблице, но не работаю. Я новичок в nodejs, так что кто-нибудь может помочь найти решение?

Если я открою этот URL, я получаю вот так: http://localhost: 3000 / api / getTableData

{"status": true, "table": {}}

table.modal. js:

const mongoose = require('mongoose');

var userSchema = new mongoose.Schema({
    product_name: {
        type: String
    },
    price: {
        type: String
    },
    catogery: {
        type: String
    }
});

mongoose.model('table', userSchema);

table.controller. js:

const mongoose = require('mongoose');
const passport = require('passport');
const _ = require('lodash');

const Table = mongoose.model('table');

var table = new Table();
/*Get Table Data*/

module.exports.getTableData = (req, res, next) => {

Table.find({ _id: req._id },
    (err, table) => {
        if (!table)
            return res.status(404).json({ status: false, message: 'Table data records not found.' });
        else
            return res.status(200).json({ status: true, table: _.pick(table, ['product_name', 'price', 'category']) });
    }
); 
 }

index.router. js:

const express = require('express');
const router = express.Router();  
const ctrlTable = require('../controllers/table.controller'); 
router.get('/getTableData', ctrlTable.getTableData); 
module.exports = router;
...