我无法在说为null的变量中获得js响应

时间:2019-06-27 13:51:32

标签: laravel api vue.js axios

当我在get方法中连接轴心时,我得到了我的api的响应,我想在网站上打印信息。

我试图更改此行:

 .then(response => (this.username= response.data.username)) or this
 .then(response => (this.username= response.data[0].username)) or this 
 .then(response => (this.username= response.data.username[0]))

脚本

<script>
   import axios from 'axios';

   export default {
     name: "acount",
     el: '#app',
     data() {
       return {
         username: null,
         pseudo: null,
         email: null,
         date: null,
       };
   },
   mounted () {
     axios
      .get('http://127.0.0.1:8080/api/user/65', {
      headers: {
      token: ''
      }
  })
    .then(response => (this.username= response.data[0].username[0]))
    .then(response => (this.pseudo = response.data.pseudo))
    .then(response => (this.email = response.data.email))
    .then(response => (this.date = response.data.create_at))
  }
}
</script>

2 个答案:

答案 0 :(得分:0)

要链接诺言,then()中的每个函数都需要返回一个值。除此之外,我们只有在知道实际响应的情况下才能提供帮助。

new Promise(function(resolve, reject) {

  setTimeout(() => resolve(1), 1000); // (*)

}).then(function(result) { // (**)

  alert(result); // 1
  return result * 2;

}).then(function(result) { // (***)

  alert(result); // 2
  return result * 2;

}).then(function(result) {

  alert(result); // 4
  return result * 2;

});

答案 1 :(得分:0)

此箭头函数使用隐式返回的值:

__init__

这导致下一个class myDAG(DAG): ... def __enter__(): ... # Save the input dataset in version-suffixed table in BQ extract_dataset = BigQueryToPartitionTableOperator(task_id='extract_dataset', get_sql_func=self.get_sql, get_schema_func=self.get_schema, get_sql_kwargs=self.get_extract_dataset_sql_kwargs, get_schema_kwargs=self.get_extracted_table_schema_kwargs, destination_dataset_table='{}.{}'.format( self.dataset, self.extracted_table), write_disposition='WRITE_TRUNCATE', use_legacy_sql=False, bigquery_conn_id=self.gcp_conn_id) 中的.then(response => (this.username= response.data[0].username[0])) 参数等于response。为避免此类错误,可以使用ESLint no-return-assign规则。

它应该是:

then

多个this.username是不必要的,因为没有多个诺言可链接。可以将它们重写为单个.then(response => { this.username= response.data[0].username[0]; return response; })

then