質問するログイン新規登録

質問編集履歴

3

Model(user)の記述に漏れがあったため修正

2019/09/29 08:39

投稿

Shimane-man
Shimane-man

スコア6

title CHANGED
File without changes
body CHANGED
@@ -69,7 +69,7 @@
69
69
  }
70
70
  });
71
71
  module.exports = mongoose.model('Product', productSchema);
72
- };
72
+
73
73
  ```
74
74
 
75
75
  Model(user)の記述:
@@ -98,6 +98,8 @@
98
98
  ]
99
99
  }
100
100
  });
101
+
102
+ module.exports = mongoose.model('User', userSchema);
101
103
  ```
102
104
 
103
105
  ### 試したこと

2

リンク修正

2019/09/29 08:39

投稿

Shimane-man
Shimane-man

スコア6

title CHANGED
File without changes
body CHANGED
@@ -102,7 +102,7 @@
102
102
 
103
103
  ### 試したこと
104
104
  下記ドキュメントを参照しましたが、何が間違っているのか理解できませんでした。
105
- https://mongoosejs.com/docs/populate.html
105
+ [https://mongoosejs.com/docs/populate.html](https://mongoosejs.com/docs/populate.html)
106
106
 
107
107
  ### 補足情報(FW/ツールのバージョンなど)
108
108
 

1

該当コードの追加

2019/09/28 17:20

投稿

Shimane-man
Shimane-man

スコア6

title CHANGED
File without changes
body CHANGED
@@ -28,7 +28,7 @@
28
28
  ```
29
29
 
30
30
  ### 該当のソースコード
31
-
31
+ Contorollerの記述:
32
32
  ```js
33
33
  exports.getProducts = (req, res, next) => {
34
34
  Product.find()
@@ -41,6 +41,65 @@
41
41
  };
42
42
  ```
43
43
 
44
+ Model(product)の記述:
45
+ ```js
46
+ const mongoose = require('mongoose');
47
+ const Schema = mongoose.Schema;
48
+ const productSchema = new Schema({
49
+ title: {
50
+ type: String,
51
+ required: true
52
+ },
53
+ price: {
54
+ type: Number,
55
+ required: true
56
+ },
57
+ description: {
58
+ type: String,
59
+ required: true
60
+ },
61
+ imageUrl: {
62
+ type: String,
63
+ required: true
64
+ },
65
+ userId: {
66
+ type: Schema.Types.ObjectId,
67
+ ref: 'User',
68
+ required: true
69
+ }
70
+ });
71
+ module.exports = mongoose.model('Product', productSchema);
72
+ };
73
+ ```
74
+
75
+ Model(user)の記述:
76
+ ```js
77
+ const mongoose = require('mongoose');
78
+ const Schema = mongoose.Schema;
79
+ const userSchema = new Schema({
80
+ name: {
81
+ type: String,
82
+ required: true
83
+ },
84
+ email: {
85
+ type: String,
86
+ required: true
87
+ },
88
+ cart: {
89
+ items: [
90
+ {
91
+ productId: {
92
+ type: Schema.Types.ObjectId,
93
+ ref: 'Product',
94
+ required: true
95
+ },
96
+ quantity: { type: Number, required: true }
97
+ }
98
+ ]
99
+ }
100
+ });
101
+ ```
102
+
44
103
  ### 試したこと
45
104
  下記ドキュメントを参照しましたが、何が間違っているのか理解できませんでした。
46
105
  https://mongoosejs.com/docs/populate.html