简体   繁体   English

如何将 db 查询的结果传递给 Vue?

[英]How to pass results from db query to Vue?

I have an API request that calls a SQL query within the function.我有一个在函数中调用 SQL 查询的 API 请求。 I am trying to pass the results to a .vue page.我正在尝试将结果传递给 .vue 页面。 (Using express-vue) Here is the api request (使用 express-vue)这是 api 请求

router.get('/search', (req, res, next) => {

    var keyword = ("%" + req.query.keyword + "%")
    var lat = req.query.lat
    var lng = req.query.lng

    console.log(keyword, lat, lng)

    connection.query('SELECT *, ( 3959 * acos (cos ( radians(?) )* cos( radians( lat ) )* cos( radians( lng ) - radians(?) )+ sin ( radians(?) )* sin( radians( lat ) ))) AS distance FROM job_posting where job_title like ? HAVING distance < 25', [
        lat, lng, lat, keyword
    ], function(err, rows) {

     })
  res.render('main', {
  // data: {
  //           results
  //         },
    vue: {
        meta: {
            title: 'Page Title',
        },
        components: ['myheader', 'myfooter', 'searchform', , 'results']
    }

});
})

and the Main.vue page和 Main.vue 页面

<template>
<div id="wrap">
    <div class="col-md-1"></div>
    <div id="main-container" class="container col-md-10">

        <myheader></myheader>
        <div class="col-md-4"></div>
        <div class="col-md-4">
        <searchform></searchform>
        <results></results>


        </div>
        <div class="col-md-4 text-center">

        </div>
        <div class="col-md-1"></div>


    </div>
</div>
</template>

Im trying to pass the data to this component results.vue我试图将数据传递给这个组件 results.vue

<template>

<p>Display data</p>

</template>

<script>
export default {
    data: function () {
        return {
            // dogs: 3
        }
    }
}
</script>

<style lang="css">
</style>

How do I pass the "rows" data to the results.vue page?如何将“行”数据传递到 results.vue 页面?

here is how the data is coming from the DB这是数据来自数据库的方式

[ RowDataPacket {
    id: 1,
    job_title: 'Software Engineer',
    job_desc: '<p>Ayo</p>',
    location: 'Akron, OH',
    job_type: 'Full-Time',
    salary: '',
    date_created: 2016-11-23T05:00:00.000Z,
    short_desc: 'yyoyo@gmail.com',
    apply_url: 'Ayo',
    lat: '41.084821',
    lng: '-81.515607',
    distance: 0 },
  RowDataPacket {
    id: 2,
    job_title: 'Software Engineer',
    job_desc: '<p>Ayo</p>',
    location: 'Akron, OH',
    job_type: 'Full-Time',
    salary: '',
    date_created: 2016-11-23T05:00:00.000Z,
    short_desc: 'yyoyo@gmail.com',
    apply_url: 'Ayo',
    lat: '41.084821',
    lng: '-81.515607',
    distance: 0 },
  RowDataPacket {
    id: 3,
    job_title: 'Software Engineer',
    job_desc: '<p>Ayo</p>',
    location: 'Akron, OH',
    job_type: 'Full-Time',
    salary: '',
    date_created: 2016-11-23T05:00:00.000Z,
    short_desc: 'yyoyo@gmail.com',
    apply_url: 'Ayo',
    lat: '41.084821',
    lng: '-81.515607',
    distance: 0 } ]

I don't know about express-vue, however I don't think making API calls from a route definition is a good idea.我不知道 express-vue,但是我不认为从路由定义进行 API 调用是一个好主意。

Rather pass the lat and lng parameters through to your results component, and make the API call there, using a method, or the created() hook for example.而是将latlng参数传递给您的results组件,并在那里使用方法或created()挂钩进行 API 调用。 It could make it a bit easier to change the implementation of the API in the future:它可以使将来更改 API 的实现更容易一些:

res.render('main', {
  data: {
    lat,
    lng,
  },
  vue: {
    meta: {
    title: 'Page Title',
  },
  components: ['myheader', 'myfooter', 'searchform', , '
}

EDIT编辑

If you really need to query data from the route, and assuming the rows is an array of javascript objects, you can render your component within the callback of the query:如果您确实需要从路由中查询数据,并假设rows是一个 javascript 对象数组,您可以在查询的回调中呈现您的组件:

connection.query('SELECT *, ( 3959 * acos (cos ( radians(?) )* cos( radians( lat ) )* cos( radians( lng ) - radians(?) )+ sin ( radians(?) )* sin( radians( lat ) ))) AS distance FROM job_posting where job_title like ? HAVING distance < 25', [
    lat, lng, lat, keyword
], function(err, rows) {
  res.render('main', {
    data: {
      results: rows
    },
    vue: {
      meta: {
        title: 'Page Title',
    },
    components: ['myheader', 'myfooter', 'searchform', 'results']
  }
  next(); // You probably need to call this
})

Don't forget to call next() .不要忘记调用next()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM