如何更新 Svelte 中 switch case 的值

发布于 2025-01-14 23:16:39 字数 4452 浏览 4 评论 0原文

我能够获取详细信息,它们已经被导入,但是当我编辑和提交时,它不需要。正确的信息显示在选项中,并且显示新值已提交,但它没有更新详细信息。我不确定出了什么问题。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最笨的告白 2025-01-21 23:16:39

您需要使用某个值来初始化 details - 如果不这样做,那么它就是 未定义,因此您无法设置它的任何键。

这是一个要演示的 Svelte REPL

You need to initialize the details with some value - if you don't, then it's undefined, so you cannot set any of it's keys.

Here's a Svelte REPL to demonstrate

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文