Get home page data from mongo

This commit is contained in:
LordMathis 2019-12-28 15:10:39 +01:00
parent 7e152520c2
commit f1c479c146
No known key found for this signature in database
GPG Key ID: 575849FD91CE470C
3 changed files with 69 additions and 6 deletions

View File

@ -18,6 +18,8 @@ export default class MainContainer extends Component {
delete window.__INITIAL_DATA__
}
console.log('MainContainer', data[0].other.about)
this.state = {
isLoadingBlog: !data[0].posts,
isLoadingAbout: !data[0].other.about,

View File

@ -30,6 +30,8 @@ export class ServerRenderer {
const promise = this.dataHolder.getData(req.path.split('/').pop())
promise.then((data) => {
console.log(data)
const context = [data, config]
const markup = renderToString(
<Router location={req.url} context={{ context }}>

View File

@ -1,8 +1,8 @@
import mongoose from 'mongoose'
import mongoose, { Schema } from 'mongoose'
export class MongoStorage {
constructor () {
this.Post = mongoose.model('Post', {
const PostSchema = new Schema({
filename: String,
published: String,
title: String,
@ -10,26 +10,85 @@ export class MongoStorage {
link: String,
body: String
})
PostSchema.index({ filename: 1 })
this.Post = mongoose.model('Post', PostSchema)
this.Other = mongoose.model('Other', {
const OtherSchema = new Schema({
filename: String,
body: String
})
OtherSchema.index({ filename: 1 })
this.Other = mongoose.model('Other', OtherSchema)
this.options = {
upsert: true,
useFindAndModify: false
}
}
addPost (post) {
const query = {
filename: post.filename
}
this.Post.findOneAndUpdate(query, post, this.options, (err) => {
if (err) throw err
})
}
addOther (filename, data) {
const query = {
filename: filename
}
const update = {
filename: filename,
body: data
}
this.Other.findOneAndUpdate(query, update, this.options, (err) => {
if (err) throw err
})
}
deleteFile (filepath) {
//
}
getData (reqPath) {
if (reqPath === '') {
const data = {
posts: [],
other: {}
}
return Promise.all([
this._getOther('about'),
this._getAllPosts()
]).then((res) => {
data.other.about = res[0].body
data.posts = res[1]
return data
})
} else if (reqPath === 'resume') {
return Promise.resolve({})
} else {
}
}
_getOther (filename) {
return new Promise((resolve, reject) => {
this.Other.findOne({ filename: filename }, (err, res) => {
if (err) reject(err)
resolve(res)
})
})
}
_getAllPosts () {
return new Promise((resolve, reject) => {
this.Post.find({}, (err, res) => {
if (err) reject(err)
resolve(res)
})
})
}
}