Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // model =====================================
- const mongoose = require('mongoose');
- const Schema = mongoose.Schema;
- const BookSchema = new Schema({
- title: { type: String },
- category: { type: mongoose.Schema.ObjectId, ref: 'Category' },
- pages: { type: Number },
- author: { type: mongoose.Schema.ObjectId, ref: 'Author' },
- published: { type: Date }
- }, { timestamps: true });
- module.exports = mongoose.model('Book', BookSchema);
- // mongo query ===============================
- Book
- .find({}).sort({ title: 1 })
- .populate('author')
- .populate('category')
- .exec((err, results) => {
- res.json({ results: results });
- });
- // reactjs ===================================
- contructor() {
- super();
- this.state = { dataTable: {}, authorization: 'token' };
- }
- loadDataFromServer() {
- let dataTable = {};
- const Authorization = this.state.authorization;
- axios
- .get(`${ROOT_URL}/books`, { headers: { Authorization } })
- .then((res) => {
- // initialize dataTable value & change state
- dataTable = res.data.results;
- this.setState({ dataTable });
- });
- }
- // response example ===================================
- **i want to get author id & fullname from react
- {
- "results": [
- {
- "_id": "5a6ea21602415a12bfd5f7af",
- "updatedAt": "2018-01-29T04:24:54.317Z",
- "createdAt": "2018-01-29T04:24:54.317Z",
- "published": "2010-12-11T17:00:00.000Z",
- "author": {
- "_id": "5a570490f086910c55bbbba9",
- "email": "marius@mariusgabriel.com",
- "fullname": "Marius Gabriel",
- "updatedAt": "2018-01-11T06:24:05.934Z",
- "createdAt": "2018-01-11T06:24:05.934Z",
- "__v": 0
- },
- "pages": 123,
- "category": {
- "_id": "5a56e8bfd80ed230a66ba010",
- "category_name": "Literature",
- "updatedAt": "2018-01-11T04:28:03.458Z",
- "createdAt": "2018-01-11T04:28:03.458Z",
- "__v": 0
- },
- "title": "Book IV",
- "__v": 0
- },
- {
- "_id": "5a6ea23b02415a12bfd5f7b0",
- "updatedAt": "2018-01-29T04:25:31.113Z",
- "createdAt": "2018-01-29T04:25:31.113Z",
- "published": "2002-01-02T17:00:00.000Z",
- "author": {
- "_id": "5a572bb0fea5b82f322d36c2",
- "email": "robi@robi.com",
- "fullname": "Robi",
- "updatedAt": "2018-01-18T10:53:51.828Z",
- "createdAt": "2018-01-11T08:27:55.183Z",
- "__v": 0
- },
- "pages": 222,
- "category": {
- "_id": "5a56e8d4d80ed230a66ba012",
- "category_name": "Fantasy",
- "updatedAt": "2018-01-11T04:28:03.458Z",
- "createdAt": "2018-01-11T04:28:03.458Z",
- "__v": 0
- },
- "title": "Book II",
- "__v": 0
- }
- ]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement