【问题标题】:How do I pass id from another const to another const in JavaScript?如何在 JavaScript 中将 id 从另一个 const 传递给另一个 const?
【发布时间】:2021-10-25 16:51:34
【问题描述】:

我有 2 个用于读取数据的 Get API I。我使用的第一个 API 是读取学生详细信息。在这个学生详细信息中,我需要从第二个 API 读取 ID 并在第一个 API 中显示数据

第一个 API 是 getDetails 第二个 API 是 getClass

我想在获得类名后将e.student_class_id 传递给getClass 我将在getDetails 处传回类名

const getDetails= (infoID) =>
    {
        axios.get(`${apiInfo}student/getDetails?student_id=${infoID}`)
        .then(function (response) { 
            if(response.data.status == 'success')
            {
                
                e = response.data.result;

                //passing this e.student_class_id;
                $('#tabClassName').html(`<div class="col-xl-12 row">
                                            <div class="col-xl-6">
                                                <h6>Class details</h6>
                                                <p>Class :<span class="item"> //and pass the class name here ? </span></p>
                                            </div>
                                        </div>
                                        `);
            }
        });
    }

    const getClass= () =>
    {
        axios.get(`${apiInfo}getClass`)
        .then(function (res) { 
            if(res.data.status == 'success')
                 {
                    res.data.result.map((e) =>{
                         if(e.id == 'how do I pass it the e.student_class_id here ?'){
                             // return``;
                            console.log(e.class_name);
                         }
                    });
            }
        });
    }

【问题讨论】:

  • 你可以使用会话
  • 会话?我如何通过 session 传递它?

标签: javascript jquery arrays axios get


【解决方案1】:

你必须经过几个步骤。

首先你需要在then方法中返回e.student_class_id

其次,您需要返回整个 "axios.get..." 链。

通过这样做,您的 getDetails 会返回一个承诺,该承诺会解析为“e.student_class_id”。

    const getDetails = (infoID) => {
  return axios
    .get(`${apiInfo}student/getDetails?student_id=${infoID}`)
    .then(function (response) {
      if (response.data.status == "success") {
        e = response.data.result;

        //passing this e.student_class_id;
        $("#tabClassName").html(`<div class="col-xl-12 row">
                                        <div class="col-xl-6">
                                            <h6>Class details</h6>
                                            <p>Class :<span class="item"> //and pass the class name here ? </span></p>
                                        </div>
                                    </div>
                                    `);
        return e.student_class_id;
      }
    });
};

结果会是这样的:

getDetail(1233).then(result=>.... // result is e.student_class_id

我们还需要编辑getClass函数。

首先它必须接受类参数。

那么,如果你想在其他地方使用你在getClass中得到的数据,你必须在then方法中返回那个值,你需要返回axios.get。就像getDetail一样:

          const getClass = (classId) => {
  return axios.get(`${apiInfo}getClass`).then(function (res) {
    if (res.data.status == "success") {
      res.data.result.map((e) => {
        if (e.id == "how do I pass it the e.student_class_id here ?") {
          // return``;
          console.log(e.class_name);
        }
      });
    }
    return res; // or anything else that you may need
  });
};

终于可以使用这些功能了

getDetails(123123).then(classId=>getClassId(classId)).then(dataFrom2ndApi=>...)

【讨论】:

  • 我不太明白getDetail(1233).then(result=&gt;.... // result is e.student_class_id 和这部分getDetails(123123).then(classId=&gt;getClassId(classId)).then(dataFrom2ndApi=&gt;...) 我应该把这些行放在哪里?
  • 我在添加return e.student_class_id时得到了e is not defined
  • 您应该在需要时放置这些行,例如,您从输入中获取“id”,然后在我的答案中使用该 id 的最后一行。关于错误,我编辑了 getDetail 函数。在getClass函数中,返回什么和返回哪里,由你决定,最重要的是你的返回值应该在“then”方法中
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多