【问题标题】:Request - Return an element请求 - 返回一个元素
【发布时间】:2020-03-20 03:20:45
【问题描述】:

喂!

所以让我们从输入我的代码和我遇到的错误开始。

这是我的代码:

login(token = 'string') {
         if (!token) throw "Veuillez entrer un token"
         request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                return "Logged in with "+token+" as "+body.username
            }
        );
     }

这是我用来测试我的客户的代码:

const c = require('./client/clienttest')
const client = new c()

let t=client.login("APITESTKORO")
console.log(t)

最后,登录控制台的东西。没那么难,就是undefined...

这就是我需要帮助的原因。我想登录Logged in with (token) as (username)

希望有人能帮帮我!

【问题讨论】:

标签: javascript node.js api request


【解决方案1】:

尝试使用:

login(token = 'string',callback) {
         if (!token) throw "Veuillez entrer un token"
         request(
            {
                url : this.baseURL+this.endpoints[2],
                headers : {
                    "auth" : token
                }
            },
            function (err, response, body) {
                // Do more stuff with 'body' here
                if (err) throw err
                if (body.status == 401) throw "Le token "+token+" est invalide."
                this.token = token
                return callback("Logged in with "+token+" as "+body.username);
            }
        );
     }

这是测试我的客户端的代码:

const c = require('./client/clienttest')
const client = new c()
client.login("APITESTKORO",function(response){
    // Here you have access to your variable
    console.log(response);
})

希望对你有帮助。

【讨论】:

    【解决方案2】:

    你也可以试试yield, 尝试使用:

    login(token = 'string') {
             if (!token) throw "Veuillez entrer un token"
             request(
                {
                    url : this.baseURL+this.endpoints[2],
                    headers : {
                        "auth" : token
                    }
                },
                function (err, response, body) {
                    // Do more stuff with 'body' here
                    if (err) throw err
                    if (body.status == 401) throw "Le token "+token+" est invalide."
                    this.token = token
                    return "Logged in with "+token+" as "+body.username
                }
            );
         }
    

    并且,对于客户端测试:

    const c = require('./client/clienttest')
    const client = new c()
    
    let t= yield client.login("APITESTKORO")
    console.log(t)
    

    【讨论】:

      【解决方案3】:

      终于,我的朋友 Ness 找到了!感谢他!

      所以,我不想让它异步,让登录、执行等功能更容易。

      这是他发给我的代码!

      login(token = 'string') {
               if (!token) throw "Veuillez entrer un token"
               return new Promise ((resolve, reject) => {
                request(
                  {
                      url : this.baseURL+this.endpoints[2],
                      headers : {
                          "auth" : token
                      }
                  },
                  function (err, response, body) {
                      // Do more stuff with 'body' here
                      if (err) throw err
                      if (body.status == 401) throw "Le token "+token+" est invalide."
                      this.token = token
                      resolve("Logged in with "+token+" as "+body.username)
                  }
              );
              })
           }
      

      并且,对于客户端测试:

      let c = require('./client/clienttest'),
      client = new c();
      
      client.login("APITESTKORO").then(result => console.log(result))
      

      再次感谢兄弟!

      【讨论】:

        猜你喜欢
        • 2013-01-28
        • 2019-05-31
        • 1970-01-01
        • 2019-01-02
        • 1970-01-01
        • 1970-01-01
        • 2021-11-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多