type-mismatch 相关问题

通常在强类型语言的上下文中发现类型不匹配错误。在不相关,不兼容或不可转换的值之间分配值时发生。例如,将字符串值分配给数字。变量或属性的类型不正确。例如,需要整数值的变量不能接受字符串值,除非整个字符串可以被识别为整数。

Excel VBA 类型不匹配日期和范围

我在循环这些数据时遇到问题,想知道您是否可以提供一些建议。 请帮助为什么我仍然遇到类型不匹配的问题。在此处输入图像描述 我的目的是提取第一个和

回答 1 投票 0

Angular 18拖放部分数据

在此component.html中: 在此组件.html中: <div cdkDropListGroup> <div cdkDropList [cdkDropListData]="meals" class="makeMeal" (cdkDropListDropped)="drop($event)"> <div *ngFor="let meal of meals" class="meal-drop"> <img src="page_delete.png" (click)="deleteFood()" title="delete" /> <h4>{{meal.name}}</h4> <label id="lblID" class="hidden">{{meal.id}}</label> </div> Add a meal: <input id="mealName" type="text" [(ngModel)]="mealName" name="mealName" required style="margin-left:10px;" /> <img src="page_white_add.png" (click)="addMeal()" /> </div> <p>My foods:</p> <div cdkDropList [cdkDropListData]="foods" class="food-grid" (cdkDropListDropped)="drop($event)"> <div *ngFor="let food of foods" class="food-item"> <img src="page_delete.png" (click)="deleteFood()" title="delete" /> <h4>{{ food.name }}</h4> <ul> --> bunch of data <-- </ul> </div> </div> 我想拖动食物项目,然后将项目名称及其 ID 添加到餐食项目中 我已经尝试过这个,但它不起作用,因为我无法克服数据不匹配的问题: drop(event: CdkDragDrop<Foods[] | Meals[]>) { if (event.previousContainer === event.container) { moveItemInArray(event.container.data, event.previousIndex, event.currentIndex); } else { if (event.previousContainer.id === 'cdk-drop-list-foods') { const foodItem = event.previousContainer.data[event.previousIndex]; const mealIndex = event.container.data.findIndex(meal => meal.id === event.container.id); if (mealIndex !== -1) { this.meals[mealIndex].foods.push(foodItem); } } else { const mealIndex = this.meals.findIndex(meal => meal.id === event.previousContainer.id); const foodItem = this.meals[mealIndex].foods[event.previousIndex]; this.meals[mealIndex].foods.splice(event.previousIndex, 1); // Add food back to foods array this.foods.push(foodItem); } } } export interface Foods { name: string, calories: number, protein: number carbs: number, potassium: number, id: number } export interface Meals { name: string, id: string, foods: Foods[] } 我真的只是想从 Food 中删除名称和 ID。 这很难查找,为此我想发布一个答案。这就是我们正在做的事情: 您有一系列食物,并且您有一系列含有食物的餐食。 您想在膳食中添加食物: export interface Foods { name: string, calories: number, protein: number carbs: number, potassium: number, id: string, isActive: boolean } export interface Meals { name: string, id: string, calCount: number, foods: Foods[] } 这其中的关键是一开始就将食物集合添加到膳食集合中。 然后,您将在 HTML 组件中设置数组来接受数据。这将让您将一种类型的数据合并到另一种类型中。 private draggedFoodItem: Foods | null = null; onTaskDragStart(event: any, food: Foods) { this.draggedFoodItem = food; event.dataTransfer.setData('text/plain', JSON.stringify(food)); } onTaskDragOver(event: any) { event.preventDefault(); } onTaskDrop(event: any, meal: Meals) { event.preventDefault(); if (this.draggedFoodItem) { // Add the dragged food item to the meal's food list meal.foods.push(this.draggedFoodItem); // Remove the dragged food item from the original foods list this.foods = this.foods.filter(food => food.id !== this.draggedFoodItem?.id); // Clear the dragged item reference this.draggedFoodItem = null; const totalCalories = meal.foods.reduce((sum, food) => sum + (food.calories || 0), 0); meal.calCount = totalCalories; } } 代码的其余部分(我这里有所有相关组件,除了处理用户的组件之外)在保存数据、离开并返回后将数据重新加载到页面。 这会将数据写入 API,我没有包含该 API,因为这几乎只是关于拖放功能 组件.css: .container { margin-left: auto; margin-right: auto; max-width: 400px; text-align: center; } .container input[type=text] { width: 150px; } div.settings { display: grid; grid-template-columns: max-content max-content; grid-gap: 5px; } div.settings label { text-align: right; } div.settings label:after { content: ":"; } .food-item { padding: 5px; border: 1px solid #ccc; background-color: #f9f9f9; text-align: left; position: relative; } .food-item ul{ list-style:none; padding:0; } .food-item ul li{ padding:0; white-space:nowrap; font-size:small; } .food-item ul li ul li{ display:inline-block; margin-right:10px; min-width:70px; } .food-item img { width: 25px; height: 25px; position:absolute; top:1px; right:1px; z-index:5; } .food-item div{ padding: 10px; } .meal-item { padding: 5px; border: 1px solid #ccc; background-color: #f9f9f9; text-align: left; position: relative; width: 90%; min-height:30px; } .meal-item div { border: 1px green solid; border-radius: 15px; margin-left: 5px; margin-top: 2px; padding: 5px; } .makeMeal{ } .makeMeal img{ width:25px; height:25px; } .meal-drop{ border:1px solid red; position: relative; min-height:25px; } .meal-drop img{ height:15px; width:15px; position:absolute; top: 2px; right: 2px; } .meal-drop div{ clear:both; width:fit-content; height:auto; } .meal-drop button{ position:absolute; bottom: 2px; right: 6px; width: fit-content; } .hidden{ display:none; } .target{ border: 1px blue solid; min-width:90%; min-height:25px; } .plainList{ list-style:none; } .plainList ul { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; padding:10px; } .plainList li{ padding:10px; position:relative; display:inline-block; margin-left:5px; margin-bottom:5px; border-radius:15px; } .plainList li img{ height:20px; width:20px; position:absolute; top:1px; right:1px; cursor:pointer; } .hideFood{ display:none; } .showFood{ display:block; border: 1px solid purple; } 组件.html: <div class="container"> <h2>Build A Meal:</h2> <p>My meals:</p> <div cdkDropListGroup> <div class="makeMeal"> <ul class="plainList"> @for(meal of meals; track meal.id; let i = $index){ <li class="meal-item"> {{meal.name}}<img src="page_delete.png" (click)="deleteMeal(meal.id, i)" /> <div class="meal-drop" [attr.draggable]="true" (dragover)="onTaskDragOver($event)" (drop)="onTaskDrop($event, meal)"> @for(droppedFood of meal.foods; track droppedFood.id; let j = $index){ <div>{{ droppedFood.name }}<img src="page_delete.png" (click)="deleteMealFood(meal.id, i, j)" /></div> } <button (click)="mealSave(meal)">Save</button> </div> Total calores: {{meal.calCount}} </li> } @empty{ <li>No meals found</li> } </ul> Add a meal: <input id="mealName" type="text" [(ngModel)]="mealName" name="mealName" required style="margin-left:10px;" /> <img src="page_white_add.png" (click)="addMeal()" /> </div> </div> <p>My foods:</p> <div class="food-grid"> <ul class="plainList"> @for(food of foods; track food.id; let i = $index){ <li class="food-item" [attr.draggable]="true" (dragstart)="onTaskDragStart($event, food)"> <div (click)="toggleActive(food)"> <img src="page_delete.png" (click)="deleteFood(food.id, i)" /> {{food.name}} <div [ngClass]="food.isActive? 'showFood' : 'hideFood'"> calories: {{food.calories}}<br /> protein: {{food.protein}}<br /> carbs: {{food.carbs}}<br /> potassium: {{food.potassium}} </div> </div> </li> } @empty{ <li>No food items found</li> } </ul> </div> </div> 组件.ts: import { Component, OnInit, NgModule, ChangeDetectorRef } from '@angular/core'; import { Router } from '@angular/router'; import { AuthService } from '../services/auth.service'; import { SaveMealService, Foods, Meals } from '../services/save-meal.service'; import { FormsModule } from '@angular/forms'; import { CommonModule } from '@angular/common'; @Component({ selector: 'app-build-meal', standalone: true, imports: [FormsModule, CommonModule], templateUrl: './build-meal.component.html', styleUrl: './build-meal.component.css' }) export class BuildMealComponent { private isUserLoggedIn: boolean = false; private userId: string = ''; private bmr: number = 0; public message: string = ''; public calCount: number = 0; public foods: Foods[] = []; public meals: Meals[] = []; public mealName: string = ''; private draggedFoodItem: Foods | null = null; public isActive = false; constructor( private authService: AuthService, private foodService: SaveMealService, private router: Router, private cdRef: ChangeDetectorRef ) { } ngAfterViewChecked() { this.cdRef.detectChanges(); } ngOnInit() { if (this.authService.isLoggedIn()) { this.isUserLoggedIn = true; var logUser = this.authService.getUserInfo(); this.userId = this.authService.userID; this.bmr = this.authService.bmr; this.getFood(); this.getMeals(); } } addMeal() { if (this.mealName.trim()) { this.foodService.createMeal(this.userId, this.mealName).subscribe({ next: (response: number) => { this.message = 'Meal created successfully'; this.getMeals(); // Refresh meal list after adding this.mealName = ''; // Clear the input field }, error: (err: number) => { this.message = 'Error creating meal: ' + err; } }); } } deleteFood(id: string, index: number) { this.foodService.removeFood(this.userId, id).subscribe({ next: (response: any) => { console.log(response); this.foods.splice(index, 1); } }); } deleteMeal(id: string, meal_index: number) { this.foodService.removeMeal(this.userId, id); this.meals.splice(meal_index, 1); } deleteMealFood(meal_id: string, meal_index: number, food_index: number) { const id = parseInt(meal_id); const foodId = this.meals[id].foods[food_index]; this.foodService.RemoveMealFood(meal_id, foodId.toString()); } getFood() { this.foodService.getAllFood(this.userId).subscribe({ next: (response: Foods[]) => { this.foods = response; }, error: (err: any) => { this.message = 'Error retrieving food: ' + err; } }); } getMeals() { this.foodService.getAllMeals(this.userId).subscribe({ next: (response: Meals[]) => { // Initialize foods array if not already present this.meals = response.map(meal => ({ ...meal, foods: meal.foods || [] // Initialize as an empty array if undefined })); }, error: (err: any) => { this.message = 'Error retrieving meals: ' + err; } }); } mealSave(meal: Meals) { this.foodService.saveMeal(meal, meal.id).subscribe(results => { console.log('All requests completed:', results); }); } onTaskDragStart(event: any, food: Foods) { this.draggedFoodItem = food; event.dataTransfer.setData('text/plain', JSON.stringify(food)); } onTaskDragOver(event: any) { event.preventDefault(); } onTaskDrop(event: any, meal: Meals) { event.preventDefault(); if (this.draggedFoodItem) { // Add the dragged food item to the meal's food list meal.foods.push(this.draggedFoodItem); // Remove the dragged food item from the original foods list this.foods = this.foods.filter(food => food.id !== this.draggedFoodItem?.id); // Clear the dragged item reference this.draggedFoodItem = null; const totalCalories = meal.foods.reduce((sum, food) => sum + (food.calories || 0), 0); meal.calCount = totalCalories; } } toggleActive(food: any) { food.isActive = !food.isActive; } trackById(index: number, item: any): number { return item.id; } } 节省餐食.service.ts: import { Injectable } from '@angular/core'; import { HttpClient, HttpErrorResponse, HttpHeaders } from '@angular/common/http'; import { forkJoin, Observable, of, throwError } from 'rxjs'; import { catchError, map, switchMap } from 'rxjs/operators'; import { jwtDecode } from "jwt-decode"; import { tap } from 'rxjs/operators'; export interface Foods { name: string, calories: number, protein: number carbs: number, potassium: number, id: string, isActive: boolean } export interface Meals { name: string, id: string, calCount: number, foods: Foods[] } @Injectable({ providedIn: 'root' }) export class SaveMealService { private apiUrl = 'https://127.0.0.1:7010/api/Food'; private apiMealUrl = 'https://localhost:7010/api/Meal'; private apiTemplateUrl = 'https://127.0.0.1:7010/api/MealTemplate' constructor( private http: HttpClient ) { } createMeal( userId: string, mealName: string ): Observable<number> { const meal = { userId, mealName }; return this.http.post<number>(`${this.apiMealUrl}/CreateMeal`, meal).pipe( tap(response => { }) ); } removeFood(userid: string, id: string ): Observable<any> { const food = { id }; return this.http.post<any>(`${this.apiUrl}/removeFood?id=${id}`, { headers: { 'Content-Type': 'application/json' }, observe: 'response' // Observe the full HTTP response }).pipe( map(response => { if (response.status === 200) { // Check for 'OK' status // Perform navigation to login page here (optional) console.log(response); return response.body as number; // Return true if the response status is 200 OK } return -2; }), catchError(() => of(-3)) // Handle error and return false ); } removeMeal(userId: string, id: string) { const item = { userId, id }; return this.http.post<any>(`${this.apiMealUrl}/removeMeal`, item, { headers: { 'Content-Type': 'application/json' }, observe: 'response' // Observe the full HTTP response }).pipe( tap(response => { }) ); } saveMeal(meal: Meals, mealId: string) { const foods: Foods[] = meal.foods; const requests = foods.map(item => { const foodId = item.id.toString(); // Ensure foodId is a string return this.http.post<any>(`${this.apiTemplateUrl}/SaveMealAsync?foodId=${foodId}&mealId=${mealId}`, { // Wrap menuItem in an "item" property headers: { 'Content-Type': 'application/json' }, observe: 'response' }).pipe( map(response => response.status === 200), catchError(err => { this.errorHandler(err); return [false]; // Return false if an error occurs }) ); }); return forkJoin(requests); // Return an observable that completes when all requests are done } errorHandler(error: HttpErrorResponse) { console.log(error.message); return throwError(error.message || "server error."); } getAllFood(userID: string): Observable<Foods[]> { return this.http.get<Foods[]>(`${this.apiUrl}/getFoods?id=${userID}`).pipe( tap(response => { }) ); } getAllMeals(userId: string): Observable<Meals[]> { return this.http.get<Meals[]>(`${this.apiMealUrl}/getMeals?userID=${userId}`).pipe( switchMap((meals: Meals[]) => { // For each meal, get the associated foods const mealsWithFoods$ = meals.map(meal => this.http.get<Foods[]>(`https://127.0.0.1:7010/api/MealTemplate/getFoodsByMeal?mealId=${meal.id}`) .pipe( map(foods => { // Calculate the total calorie count for the meal const totalCalories = foods.reduce((acc, food) => acc + food.calories, 0); return { ...meal, foods, calCount: totalCalories }; // Include calCount in the meal object }) ) ); // Combine all observables into one return forkJoin(mealsWithFoods$); }), tap(response => { console.log('Meals with foods and calorie counts:', response); }) ); } RemoveMealFood( meal_id: string, foodId: string): Observable<any> { return this.http.post<any>(`${this.apiTemplateUrl}/RemoveFood?mealId=${meal_id}&foodId=${foodId}`, { headers: { 'Content-Type': 'application/json' }, observe: 'response' // Observe the full HTTP response }).pipe( map(response => { if (response.status === 200) { // Check for 'OK' status // Perform navigation to login page here (optional) console.log(response); return response; } return false; }), catchError(() => of(false)) ); } saveFood( userid: string, foodName: string, calories: number, carbohydrates: number, protein: number, potassium: number, servingSize: number, portion: number ): Observable<number> { const food = { userid, foodName, calories, protein, carbohydrates, potassium, servingSize, portion }; return this.http.post<any>(`${this.apiUrl}/SaveFood`, food, { headers: { 'Content-Type': 'application/json' }, observe: 'response' // Observe the full HTTP response }).pipe( map(response => { if (response.status === 200) { // Check for 'OK' status // Perform navigation to login page here (optional) console.log(response); return response.body as number; // Return true if the response status is 200 OK } return -2; }), catchError(() => of(-3)) // Handle error and return false ); } }

回答 1 投票 0

无法将数据(类型接口{})转换为类型字符串:需要类型断言

我是个新手,我正在玩这个通知包。 起初我的代码如下所示: func doit(w http.ResponseWriter, r *http.Request) { notify.Post("my_event", "你好...

回答 6 投票 0

出现错误“原因:无法确定颜色类型。类型不匹配:无法用 str 替换 Call”

我有一项任务是为标志创建成本。我已经完成了作业的所有工作,代码甚至按照它想要的方式完美运行。但是,它一直给我错误“原因:

回答 1 投票 0

从数组返回动态值的公式在 Excel 中有效,但在 VBA 中无效

我在 Excel 中编写的公式有问题,该公式要在列中取负数(在本例中为 M10),并找到一个可以吸收它而不低于 0 的正数。(例如10 + (5...

回答 1 投票 0

设置范围以从一个工作表读取值并写入另一个工作表

该脚本的目的是: 逐个单元格读取工作表 NVL 中 LADUNGSNUMMER 列中的值。 查找工作表 DATA 的 Transport 列中的每个值。 从 co 中读取相应的值...

回答 1 投票 0

MS access 中子表单上的日期和类别之间的多重过滤器 >> 类型不匹配错误

请任何人建议我解决这个错误,我是 MS Access 开发的新手。我在过滤日期和类别之间过滤子表单时遇到问题。它显示错误声明...

回答 1 投票 0

VBA。将 A1 = Transpose(range(“a1-n1”) 传递给函数

所以第一个数组工作正常,但是当我将数组传递给过程时,我得到数组的类型不匹配。 我显然不明白。 过程1() Dim Arr 作为变体 到达 = 应用程序。

回答 1 投票 0

根据单元格值(日期)隐藏行:类型不匹配

我正在监控维护进度,并且想要隐藏单元格值(日期)> = 45 天前的行。 在 S 列上设置日期时,我得到: 运行时错误13:类型不匹配。 夏娃...

回答 1 投票 0

为什么我的类型不匹配的 C++ 代码使用 GCC“-Wall”标志进行编译时没有警告?

我有以下代码: #包括 char add(char a, int b) { 返回a+b; } int 主函数(无效) { 长长a = 100000000; 长长b = 20; int x = add(a, b); ...

回答 3 投票 0

vba 类型日期不匹配

我正在创建一个excel来监控维护进度。我想隐藏基于单元格值(日期)超过 45 天的行。 我已经在 S 列上设置了日期,但它是运行时的

回答 1 投票 0

在 MS Access 中选择日期时数据类型不匹配

我想在MS Access中查询两个日期之间的记录。日期字段是一个字符串,我使用下面的函数将其转换为日期格式。 devicelocaltime 作为字符串传入,并且

回答 1 投票 0

VBA:在数组中使用递归公式

出于性能原因,我将几列分配到数组中并将结果复制回我的工作表。这对于某些函数来说效果很好,但对于最后一个函数则不然。 在 Excel 中,该函数...

回答 1 投票 0

如果if语句为假,VBA为什么不继续循环

如果满足条件,我正在尝试复制某些范围。但是当语句变为 false 时,我得到了运行时错误 13:类型不匹配。如果该陈述是错误的,那么它不应该执行任何操作并...

回答 1 投票 0

动态范围参考中的类型不匹配

我试图根据数据输入表中的名称在表中查找一行,然后找到该行中的第一个打开的单元格,最后将数据输入表中的日期复制到该单元格中。 我的代码将

回答 1 投票 0

Skunk - Scala - 多参数查询出错

我收到以下代码错误 导入 br.com.gbtech.model.PaymentRequestItem 导入 cats.effect.{IO,资源} 导入 skunk.codec.all.* 导入 skunk.implicits.* 导入臭鼬。{查询,Sess...

回答 2 投票 0

代码在一个子项中给出类型不匹配,但在另一个子项中没有给出类型不匹配

您好,我有一个用户表单,其中包含 3 个组合框:TaskStartTime、TaskFinishTime 和 Task_Duration。 计划是您更改用户表单中的开始或结束值,并且持续时间值发生变化...

回答 1 投票 0

我不断收到类型不匹配的信息,将列类型设置为整数

根据建议,我将正在使用的列设置为 Const 引用。 只要我不将列声明为整数或长整型,此操作就有效。 Integer 或 Long 返回“编译错误,类型不匹配...

回答 1 投票 0

VBA 脚本从一个工作表读取值并写入另一个工作表(设置范围问题)

我正在努力处理 VBA 子例程,希望得到一些帮助。该脚本旨在执行以下任务: 从工作表中的“LADUNGSNUMMER”列读取值...

回答 1 投票 0

如何在 Flutter 中为地图分配自定义颜色而不出现类型不匹配错误?

我正在 Android Studio 中开发 Flutter 应用程序,并面临自定义颜色分配的挑战。我的目标是为我的应用程序中的不同状态分配特定的颜色。对于一些统计数据...

回答 1 投票 0

© www.soinside.com 2019 - 2024. All rights reserved.