Nodejs total noob here.
Trying to post a Mongoose DB query through json from an express server to a REACT app, currently achieving this via a .get shown below.
app.get('/posty', (req,res) => {
if (req.session.user && req.cookies.user_id) {
Blog.find({name : "my_user"}).exec(function(err, blogs) {
if (err) throw err;
res.json([
{name : req.session.user.name, text : blogs},
]);
})
} else {
res.redirect('/login');
}
});
My react App.js looks like this
import logo from './logo.svg';
import './App.css';
import Texts from './components/texts/texts';
import React, { Component } from 'react';
class App extends Component {
state = { posty: []}
componentDidMount() {
fetch('/posty')
.then(res => res.json())
.then(posty => this.setState({posty}));
}
render() {
return (
<div className="App">
<header className="App-header">
<Texts> </Texts>
<ul>
{
this.state.posty.map(post =>
<li key={post.name}>{post.text[0].content } </li>
)}
</ul>
</header>
</div>
);
}
}
export default App;
This achieves what you'd expect, the outputs the Blogs array at index 0.
What I'm trying to achieve is an array that iterates over the Blog json data, and outputs ALL .contents, not just for Blog[0].
This may seem like a simple question but I am a total NodeJS noob! I apologize!
If you have any general advice other than this I am all ears, thank you!