如何更新 Svelte 中 switch case 的值
我能够获取详细信息,它们已经被导入,但是当我编辑和提交时,它不需要。正确的信息显示在选项中,并且显示新值已提交,但它没有更新详细信息。我不确定出了什么问题。
export let details;
const handleSubmit = (event) => {
const target = event.target;
const values = {
TaskFacility: target.TaskFacility.value,
name: target.name.value,
inspector: target.inspector.value,
desc: target.details.value,
type: target.inspectionClass.value
};
if(values.type){
switch(values.type){
case 'annual':
details.annual = true;
details.quarterly = false;
details.monthly = false;
details.weekly = false;
seriesPossible = true;
break;
case 'quarterly':
details.quarterly = true;
details.annual = false;
details.monthly = false;
details.weekly = false;
seriesPossible = true;
break;
case 'monthly':
details.monthly = true;
details.quarterly = false;
details.annual = false;
details.weekly = false;
seriesPossible = true;
break;
case 'weekly':
details.weekly = true;
details.quarterly = false;
details.annual = false;
details.monthly = false;
seriesPossible = true;
break;
case 'none':
details.weekly = false;
details.quarterly = false;
details.annual = false;
details.monthly = false;
seriesPossible = false;
break;
}
}
Meteor.call('updateInspectionDetails', details._id, values, (error) => {
if(error){
Swal.fire('ERROR', error.message, 'error');
} else{
Swal.fire({
title: "Details Updated!",
icon: "success"
}).then((result) => {
if(result.value){
editDetails = false;
location.reload();
}
});
}
});
}
}).catch(err => errors = extractErrors(err));
};
switch case 和 Meteor.call 之间有验证代码。 (如果我需要发布,我会的) 这是 html:
<div class="form-group col-sm-12 col-md-6">
<label for="inspectionClass">Choose Inspection Type *</label>
<select name="inspectionClass" id="inspectionClass" class="form-control" >
<option value="" diabled>Choose Inspection Type *</option>
{#if details.annual || details.quarterly || details.monthly || details.weekly}
{#if details.annual}
<option value="annual" selected>Annual</option>
{/if}
{#if details.quarterly}
<option value="quarterly" selected>Quarterly</option>
{/if}
{#if details.monthly}
<option value="monthly" selected>Monthly</option>
{/if}
{#if details.weekly}
<option value="weekly" selected>Weekly</option>
{/if}
{:else}
<option value="none" selected>None</option>
{/if}
<option value="none">None</option>
<option value="annual">Annual</option>
<option value="quarterly">Quarterly</option>
<option value="monthly">Monthly</option>
<option value="weekly">Weekly</option>
</select>
</div>
方法:
updateInspectionDetails: (detailsId, updateObj) => {
if(detailsId)
InspectionDetails.update(detailsId, {
$set: updateObj
});
}
},
记录详细信息:
TaskFacility: "8zAeRMdxkH7xdj7pS"
annual: false
clientId: "g34xP9xKyWHGEDevy"
createdAt: Mon Feb 15 2021 10:38:30 GMT-0500 (Eastern Standard Time) {}
duration: "1.5"
inspectionSigned: false
inspector: "XYtN6Ph7RNy7g26Y7"
inspectorImage: false
monthly: true
name: "Monthly"
published: true
quarterly: false
startDate: Mon Feb 22 2021 11:00:00 GMT-0500 (Eastern Standard Time) {}
taskId: "JCdbBuGak3CvTYyPs"
weekly: false
_id: "WQ6cp6LZpSnncEiQ8"
每次我尝试“绑定”该值时,它都是未定义的。我缺少什么? 谢谢!!
I am able to get the details, they are already being imported, but when I edit and submit, it doesn't take. The right information is being displayed in the options and it's showing that the new value gets submitted, but It's not updating the details. I'm not sure what is wrong.
export let details;
const handleSubmit = (event) => {
const target = event.target;
const values = {
TaskFacility: target.TaskFacility.value,
name: target.name.value,
inspector: target.inspector.value,
desc: target.details.value,
type: target.inspectionClass.value
};
if(values.type){
switch(values.type){
case 'annual':
details.annual = true;
details.quarterly = false;
details.monthly = false;
details.weekly = false;
seriesPossible = true;
break;
case 'quarterly':
details.quarterly = true;
details.annual = false;
details.monthly = false;
details.weekly = false;
seriesPossible = true;
break;
case 'monthly':
details.monthly = true;
details.quarterly = false;
details.annual = false;
details.weekly = false;
seriesPossible = true;
break;
case 'weekly':
details.weekly = true;
details.quarterly = false;
details.annual = false;
details.monthly = false;
seriesPossible = true;
break;
case 'none':
details.weekly = false;
details.quarterly = false;
details.annual = false;
details.monthly = false;
seriesPossible = false;
break;
}
}
Meteor.call('updateInspectionDetails', details._id, values, (error) => {
if(error){
Swal.fire('ERROR', error.message, 'error');
} else{
Swal.fire({
title: "Details Updated!",
icon: "success"
}).then((result) => {
if(result.value){
editDetails = false;
location.reload();
}
});
}
});
}
}).catch(err => errors = extractErrors(err));
};
There is validate code in between the switch case and the Meteor.call. (if I need to post that I will)
This is the html:
<div class="form-group col-sm-12 col-md-6">
<label for="inspectionClass">Choose Inspection Type *</label>
<select name="inspectionClass" id="inspectionClass" class="form-control" >
<option value="" diabled>Choose Inspection Type *</option>
{#if details.annual || details.quarterly || details.monthly || details.weekly}
{#if details.annual}
<option value="annual" selected>Annual</option>
{/if}
{#if details.quarterly}
<option value="quarterly" selected>Quarterly</option>
{/if}
{#if details.monthly}
<option value="monthly" selected>Monthly</option>
{/if}
{#if details.weekly}
<option value="weekly" selected>Weekly</option>
{/if}
{:else}
<option value="none" selected>None</option>
{/if}
<option value="none">None</option>
<option value="annual">Annual</option>
<option value="quarterly">Quarterly</option>
<option value="monthly">Monthly</option>
<option value="weekly">Weekly</option>
</select>
</div>
And the method:
updateInspectionDetails: (detailsId, updateObj) => {
if(detailsId)
InspectionDetails.update(detailsId, {
$set: updateObj
});
}
},
Logged Details:
TaskFacility: "8zAeRMdxkH7xdj7pS"
annual: false
clientId: "g34xP9xKyWHGEDevy"
createdAt: Mon Feb 15 2021 10:38:30 GMT-0500 (Eastern Standard Time) {}
duration: "1.5"
inspectionSigned: false
inspector: "XYtN6Ph7RNy7g26Y7"
inspectorImage: false
monthly: true
name: "Monthly"
published: true
quarterly: false
startDate: Mon Feb 22 2021 11:00:00 GMT-0500 (Eastern Standard Time) {}
taskId: "JCdbBuGak3CvTYyPs"
weekly: false
_id: "WQ6cp6LZpSnncEiQ8"
And every time I try to "bind" the value, it's undefined. What am I missing?
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用某个值来初始化
details
- 如果不这样做,那么它就是未定义
,因此您无法设置它的任何键。这是一个要演示的 Svelte REPL
You need to initialize the
details
with some value - if you don't, then it'sundefined
, so you cannot set any of it's keys.Here's a Svelte REPL to demonstrate