質問編集履歴

1

文字の追記

2020/05/13 15:44

投稿

todayyy
todayyy

スコア31

test CHANGED
File without changes
test CHANGED
@@ -81,3 +81,231 @@
81
81
 
82
82
 
83
83
  リレーションしている子のAクラスからはrelatebToを使用して検索することはできないのでしょうか?
84
+
85
+
86
+
87
+ ### 追記
88
+
89
+ 以下全容のコードになります。
90
+
91
+
92
+
93
+ ```HTML
94
+
95
+ <!DOCTYPE HTML>
96
+
97
+ <html>
98
+
99
+ <head>
100
+
101
+ <meta charset="utf-8">
102
+
103
+ <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no, viewport-fit=cover">
104
+
105
+ <meta http-equiv="Content-Security-Policy" content="default-src * data: gap: https://ssl.gstatic.com; style-src * 'unsafe-inline'; script-src * 'unsafe-inline' 'unsafe-eval'">
106
+
107
+ <script src="components/loader.js"></script>
108
+
109
+ <script src="lib/onsenui/js/onsenui.min.js"></script>
110
+
111
+
112
+
113
+ <link rel="stylesheet" href="components/loader.css">
114
+
115
+ <link rel="stylesheet" href="lib/onsenui/css/onsenui.css">
116
+
117
+ <link rel="stylesheet" href="lib/onsenui/css/onsen-css-components.css">
118
+
119
+ <link rel="stylesheet" href="css/style.css">
120
+
121
+
122
+
123
+ <script>
124
+
125
+
126
+
127
+ if (ons.platform.isIPhoneX()) {
128
+
129
+ document.documentElement.setAttribute('onsflag-iphonex-portrait', '');
130
+
131
+ document.documentElement.setAttribute('onsflag-iphonex-landscape', '');
132
+
133
+ }
134
+
135
+
136
+
137
+ </script>
138
+
139
+ <script src="test.js"></script>
140
+
141
+ </head>
142
+
143
+ <body>
144
+
145
+ <ons-navigator id="nav" page="page1.html"></ons-navigator>
146
+
147
+
148
+
149
+ <ons-template id="page1.html">
150
+
151
+ <ons-page>
152
+
153
+ <ons-button onclick="btn02();">リレーション登録</ons-button><br>
154
+
155
+ <ons-button onclick="btn03();">リレーション表示</ons-button>
156
+
157
+ </ons-page>
158
+
159
+ </ons-template>
160
+
161
+ </body>
162
+
163
+ </html>
164
+
165
+ ```
166
+
167
+
168
+
169
+ ```javascript
170
+
171
+ const appKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
172
+
173
+ const clientKey = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
174
+
175
+ var ncmb;
176
+
177
+ var currentUser;
178
+
179
+
180
+
181
+ ons.ready(function(){
182
+
183
+ ncmb = new NCMB(appKey, clientKey);
184
+
185
+ });
186
+
187
+
188
+
189
+ function btn02(){
190
+
191
+ var A_class = ncmb.DataStore('class_A');
192
+
193
+ var a_class = new A_class();
194
+
195
+ a_class
196
+
197
+ .set("text" , "hello")
198
+
199
+ .save()
200
+
201
+ .then(function(data){
202
+
203
+
204
+
205
+ var B_class = ncmb.DataStore('class_B');
206
+
207
+ var b_class = new B_class();
208
+
209
+ var item_A = new ncmb.Relation();
210
+
211
+ item_A
212
+
213
+ .add(data);
214
+
215
+ b_class
216
+
217
+ .set('tests' , item_A)
218
+
219
+ .save()
220
+
221
+ .then(function(data){
222
+
223
+ alert("完了");
224
+
225
+ })
226
+
227
+ .catch(function(err){
228
+
229
+ alert("ユーザにリレーション保存時 : " + err);
230
+
231
+ });
232
+
233
+ })
234
+
235
+ .catch(function(err){
236
+
237
+ alert(err);
238
+
239
+ });
240
+
241
+ }
242
+
243
+
244
+
245
+ function btn03(){
246
+
247
+ var b_class = ncmb.DataStore('class_B');
248
+
249
+ b_class
250
+
251
+ .equalTo("objectId" , "qTYE5CIQVId48XzL")
252
+
253
+ .fetchAll()
254
+
255
+ .then(function(result){
256
+
257
+ for(var object of result){
258
+
259
+ console.log(JSON.stringify(object));
260
+
261
+
262
+
263
+ var a_class = ncmb.DataStore('class_A');
264
+
265
+ a_class
266
+
267
+ .relatedTo(object, "class_B")
268
+
269
+ .count()
270
+
271
+ .fetchAll()
272
+
273
+ .then(function(result2){
274
+
275
+ for(var object2 of result2){
276
+
277
+
278
+
279
+ alert(object2.text);
280
+
281
+ }
282
+
283
+ alert(result2.count);
284
+
285
+ })
286
+
287
+ .catch(function(err){
288
+
289
+ alert("リレーション取得時のエラー : " + err);
290
+
291
+ });
292
+
293
+ }
294
+
295
+ })
296
+
297
+ .catch(function(err){
298
+
299
+ alert(err);
300
+
301
+ })
302
+
303
+ }
304
+
305
+ ```
306
+
307
+
308
+
309
+ 以下のコードを実行したところ、エラーは起きず、アラートでは0と表記されます。
310
+
311
+ またalert(object2.text)は表記されませんでした。