首页 > 解决方案 > VueJS 方法返回承诺。如何在模板中显示数据

问题描述

VueJS 前端使用 axios 从 Express API 中检索数据。数据作为承诺返回。我不知道如何在模板中显示它。目标是添加要在排行榜中显示的数据,所以我正在执行第一个查询以获取用户名和分数(这是一个总和),然后是第二个查询以获取每个用户最后一次活动的数据.

这是代码。

排行榜组件:

<template>
  <table class="ui celled table">
    <thead>
      <tr><th colspan="4">Leaderboard for {{ currentGroup.name }}</th></tr>
      <tr>
        <th>Username</th>
        <th>Last Action</th>
        <th>Date</th>
        <th>Score</th>
      </tr>
    </thead>
    <tbody>
      <tr v-for="(leader) in leaderboard" :key="leader.users_id">
        <td>
          <h4 class="ui image header">
            <img v-if="leader.avatar_url" :src="leader.avatar_url"  class="ui mini rounded image">
            <v-gravatar v-else :email="leader.email" class="ui mini rounded image" />
            <div class="content">
              {{leader.username}}
            </div>
          </h4>
        </td>
        <td>{{ lastActByUser(leader.users_id).deed }}</td>
        <td></td>
        <!-- <td>{{ lastAct(leader.id).deed }}</td>
        <td>{{ moment(lastAct(leader.id).created_at).strftime("%A, %d %b %Y %l:%M %p") }}</td> -->
        <td>{{leader.count}}</td>
      </tr>
    </tbody>
  </table>
</template>

<script>
import moment from 'moment-strftime'
import _ from 'lodash'
import ReportsService from '@/services/ReportsService'
import ActsService from '@/services/ActsService'
export default {
  name: "Leaderboard",
  data() {
    return {
    }
  },
  computed: {
    leaderboard () {
      return this.$store.getters.leaderboard;
    },
    currentGroup () {
      return this.$store.getters.currentGroup;
    }
    // ,
    // lastAct (userId) {
    //   return _.orderBy(this.actsByUser(userId), 'created_at')[0];
    // }
  },
  mounted () {
    this.getLeaderboard();
  },
  methods: {
    getLeaderboard: async function () {
      console.log('currentGroup: ', this.$store.getters.currentGroup)
      this.$store.dispatch("updateLeaderboard", this.$store.getters.currentGroup);
    },
    moment: function (datetime) {
      return moment(datetime);
    }
    ,
    async lastActByUser (leader_id) {
      console.log('getting last act for user')
      const response = await ActsService.fetchLastActByUser ({
        userId: leader_id
      });
      this.deed = response.data.deed
      this.created_at = response.data.created_at
      console.log('lastAct response: ', response.data)
    }
  }
};
</script>

这是 ActsService:

import Api from '@/services/Api'

export default {
  fetchActs () {
    return Api().get('acts')
  },

...

  fetchLastActByUser (params) {
    console.log('calling the last act service with userId: ', params.userId)
    return Api().post('acts/last_by_user', params)
  },

}

这里是特快路线:

const express = require('express');
const User = require('../models/User');
const auth = require('../middlewares/authenticate');
const Act = require('../models/Act');

let router = express.Router();

router.get('/', async (req, res) => {
  const acts = await Act
    .query().eager('user');
  res.json(acts);
});

...
// GET last act for specified user
router.post('/last_by_user', async (req, res, next) => {
  const lastAct = await Act
    .query()
    .findOne({
      users_id: req.body.userId
    });
  console.log('lastAct on server side is: ', lastAct)
  res.json(lastAct);
})

module.exports = router;

标签: expressvue.jsaxiosknex.js

解决方案


推荐阅读