Advertisement
nubilfi

Objects are not valid as a React child (found object)

Jan 29th, 2018
354
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // model =====================================
  2. const mongoose = require('mongoose');
  3.  
  4. const Schema = mongoose.Schema;
  5. const BookSchema = new Schema({
  6.     title: { type: String },
  7.     category: { type: mongoose.Schema.ObjectId, ref: 'Category' },
  8.     pages: { type: Number },
  9.     author: { type: mongoose.Schema.ObjectId, ref: 'Author' },
  10.     published: { type: Date }
  11. }, { timestamps: true });
  12.  
  13. module.exports = mongoose.model('Book', BookSchema);
  14.  
  15. // mongo query  ===============================
  16. Book
  17.  .find({}).sort({ title: 1 })
  18.  .populate('author')
  19.  .populate('category')
  20.  .exec((err, results) => {
  21.     res.json({ results: results });
  22.  });
  23.  
  24. // reactjs ===================================
  25. contructor() {
  26.  super();
  27.  
  28.  this.state = { dataTable: {}, authorization: 'token' };
  29. }
  30.  
  31. loadDataFromServer() {
  32.   let dataTable = {};
  33.   const Authorization = this.state.authorization;
  34.  
  35.   axios
  36.     .get(`${ROOT_URL}/books`, { headers: { Authorization } })
  37.     .then((res) => {
  38.         // initialize dataTable value & change state
  39.         dataTable = res.data.results;
  40.         this.setState({ dataTable });
  41.     });    
  42. }
  43.  
  44.  
  45. // response example  ===================================
  46.  
  47. **i want to get author id & fullname from react
  48. {
  49.  "results": [
  50.         {
  51.             "_id": "5a6ea21602415a12bfd5f7af",
  52.             "updatedAt": "2018-01-29T04:24:54.317Z",
  53.             "createdAt": "2018-01-29T04:24:54.317Z",
  54.             "published": "2010-12-11T17:00:00.000Z",
  55.             "author": {
  56.                 "_id": "5a570490f086910c55bbbba9",
  57.                 "email": "marius@mariusgabriel.com",
  58.                 "fullname": "Marius Gabriel",
  59.                 "updatedAt": "2018-01-11T06:24:05.934Z",
  60.                 "createdAt": "2018-01-11T06:24:05.934Z",
  61.                 "__v": 0
  62.             },
  63.             "pages": 123,
  64.             "category": {
  65.                 "_id": "5a56e8bfd80ed230a66ba010",
  66.                 "category_name": "Literature",
  67.                 "updatedAt": "2018-01-11T04:28:03.458Z",
  68.                 "createdAt": "2018-01-11T04:28:03.458Z",
  69.                 "__v": 0
  70.             },
  71.             "title": "Book IV",
  72.             "__v": 0
  73.         },
  74.         {
  75.             "_id": "5a6ea23b02415a12bfd5f7b0",
  76.             "updatedAt": "2018-01-29T04:25:31.113Z",
  77.             "createdAt": "2018-01-29T04:25:31.113Z",
  78.             "published": "2002-01-02T17:00:00.000Z",
  79.             "author": {
  80.                 "_id": "5a572bb0fea5b82f322d36c2",
  81.                 "email": "robi@robi.com",
  82.                 "fullname": "Robi",
  83.                 "updatedAt": "2018-01-18T10:53:51.828Z",
  84.                 "createdAt": "2018-01-11T08:27:55.183Z",
  85.                 "__v": 0
  86.             },
  87.             "pages": 222,
  88.             "category": {
  89.                 "_id": "5a56e8d4d80ed230a66ba012",
  90.                 "category_name": "Fantasy",
  91.                 "updatedAt": "2018-01-11T04:28:03.458Z",
  92.                 "createdAt": "2018-01-11T04:28:03.458Z",
  93.                 "__v": 0
  94.             },
  95.             "title": "Book II",
  96.             "__v": 0
  97.         }
  98.     ]
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement