Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
141 views
in Technique[技术] by (71.8m points)

javascript - How can I get my mongoose collection data displayed on the client side?

I am trying to fetch the data from my mongoose collection, and dispaly that data on client side. But I am getting 'Object' on the cilent side instead of data, but the data is saved in the database.

Can anyone help me, Thanks

This is my code

router.js

const mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Library', { useNewUrlParser: true });


const api_list = new mongoose.Schema({
    data:Array
});

const api = mongoose.model('api_listLs', api_list);

router.post('/api/list/Post', (request, response) => {
        const data = request.body
        const apiList = new api({data:data});
        apiList.save().then(() => {
            console.log(data)
            response.json(data);
        }).catch(() => {
            response.status(400).send("Item was not send to the database")
        });
});



router.get('/api/list/Get', (request, response) => {
    api.find({},(err,data)=> {
        if(err){
            response.end();
            return;
        }
        response.json(data);
    });
});

page1.js

I used a fetch 'POST' method to pass data into database. And save it in a collection

function TableRow() {
    let items = 'hey'
    let domore = 'hi'
    let cells = document.querySelectorAll('#recieve-info td');
    cells.forEach(cell => cell.onclick = async function () {
        let prevcell = cell.previousElementSibling;
        if (prevcell) {
            let data = {items, domore}
            let options = {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify(data)
            };
            
            const response = await fetch('/api/list', options);
            const json = await response.json();
            console.log(json);
        }
    });
}

page2.js

Here I used fetch 'GET' method to to retreive data from database and show it on client side

async function ParaG() {
  const response = await fetch('/api/list');
  const data = await response.json();
  console.log(data);
  alert(data);

  for (item of data) {
    const para = document.querySelector('.Second-Para');
    para.innerHTML += `${item.data}, ${item.data}`
  }
  
}

'.Second-Para' is a p element in a container 'div'

Here is the output. I am getting 'Object' instead of data I am getting 'object' not the value

This app pass data from one page to another by onclick

question from:https://stackoverflow.com/questions/65837732/how-can-i-get-my-mongoose-collection-data-displayed-on-the-client-side

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

result is in data key and data is array of objects so use {data} to access the data like this:

async function ParaG() {
  const response = await fetch('/api/list');
  const {data} = await response.json();
  console.log(data);
  alert(data);

  for (item of data) {
    let info = item.data[0];//access to data 
    const para = document.querySelector('.Second-Para');
    para.innerHTML += `${info.items}, ${info.domore}`
  }
  
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...