Explorar o código

0210348-数据库读取问题

0210348 hai 4 meses
pai
achega
c303c299fa

+ 2 - 1
src/app/child-page/chat/chat.page.html

@@ -22,8 +22,9 @@
 <ion-footer>
   <ion-toolbar>
     <ion-item>
-      <ion-input [(ngModel)]="userInput" placeholder="输入消息..."></ion-input>
+      <ion-textarea [(ngModel)]="userInput" placeholder="输入消息..." auto-grow></ion-textarea>
       <ion-button (click)="sendMessage()" class="sendButton">发送</ion-button>
     </ion-item>
   </ion-toolbar>
+  
 </ion-footer>

+ 4 - 2
src/app/child-page/chat/chat.page.ts

@@ -48,15 +48,16 @@ export class ChatPage implements OnInit {
     } else {
       this.username = '未登录';
     }
-    console.log("tab-ai:"+this.username);
+    console.log("tab-ai:" + this.username);
   }
 
   async loadChatHistory(chatId: number | null) {
-    if (!chatId) return;
+    if (!chatId || this.username === '未登录') return;
 
     const Chat = Parse.Object.extend('ai_chat');
     const query = new Parse.Query(Chat);
     query.equalTo('chatId', chatId);
+    query.equalTo('username', this.username); // 添加用户名条件
     query.ascending('userChatOrder');
 
     try {
@@ -123,6 +124,7 @@ export class ChatPage implements OnInit {
     const Chat = Parse.Object.extend('ai_chat');
     const query = new Parse.Query(Chat);
     query.equalTo('chatId', chatId);
+    query.equalTo('username', this.username); // 添加用户名条件
     query.descending('userChatOrder');
     query.limit(1); // 限制为1条结果以获取最后一条消息
 

+ 15 - 1
src/app/home/home.page.ts

@@ -22,7 +22,9 @@ export class HomePage implements OnInit {
     private navCtrl: NavController,
   ) {}
 
-  ngOnInit() {}
+  ngOnInit() {
+    this.loadUserData();
+  }
 
   ionViewDidEnter() {
     this.loadUserData();
@@ -52,11 +54,23 @@ export class HomePage implements OnInit {
         this.current = weightStatus.get('current') || 0;
         this.target = weightStatus.get('target') || 0;
         this.reward = this.calculateReward(this.initial, this.target);
+      } else {
+        // 没有找到用户数据时,将所有体重数据重置为0
+        this.initial = 0;
+        this.current = 0;
+        this.target = 0;
+        this.reward = 0;
       }
     } catch (error) {
       console.error('Error loading data', error);
+      // 出现错误时,将所有体重数据重置为0
+      this.initial = 0;
+      this.current = 0;
+      this.target = 0;
+      this.reward = 0;
     }
   }
+  
 
   calculateReward(initial: number, target: number): number {
     return (initial - target) * 10;