Get home page data from mongo
This commit is contained in:
parent
7e152520c2
commit
f1c479c146
|
@ -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,
|
||||
|
|
|
@ -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 }}>
|
||||
|
|
|
@ -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)
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue