express - 职位发布不会删除,不断获取 id 是“未定义”
问题描述
我的小应用程序有问题。我目前有一个工作页面,我可以发布并获得工作。
在前端我有一个按钮可以按下Delete
,但是当我按下它时,它一直给我这个错误 - DELETE FROM 'jobs' WHERE 'id' = 'undefined'
。
目前使用的技术有 MySQL、sequelize、node.js、express 和 vue.js。
<div>
<h2 class="mb-4 font-weight-light">Job postings</h2>
<div class="d-flex align-items-center justify-content-between">
<b-input-group class="w-30">
<b-form-input v-model="filter" placeholder="Type to Search" />
<b-input-group-append>
<b-btn :disabled="!filter" @click="filter = ''">Clear</b-btn>
</b-input-group-append>
</b-input-group>
<b-button variant="primary" class="d-flex align-items-center" v-b-modal.addJob><i class="material-icons mr-1"></i> Add job</b-button>
</div>
<b-table responsive hover :items="jobs" :fields="fields" :filter="filter" no-sort-reset sort-by="postedOn" :sort-desc="true" class="mt-3 f6">
<template slot="job_postingURL" slot-scope="data">
<a :href="`${data.value}`" target="_blank">{{ data.value }}</a>
</template>
<template slot="Remove" scope="jobs">
<b-btn variant="danger" @click="deleteJob(jobs.ID)"> Delete </b-btn>
</template>
</b-table>
<add-job></add-job>
</div>
</template>
<script>
import AddJob from '@/components/jobs/AddJob'
import JobService from '../../services/JobService'
import axios from 'axios'
export default {
components: {
AddJob
},
data () {
return {
fields: [
{ Key: 'ID', label: 'Job ID', sortable: false},
{ key: 'job_title', label: 'Job title', sortable: true },
{ key: 'job_name', label: 'Company name', sortable: true },
{ key: 'job_location', label: 'Location', sortable: true },
{ key: 'job_postingURL', label: 'Job posting link', sortable: false },
{ key: 'job_postingOn', label: 'Posted on', sortable: true, tdClass: 'text-right' },
{ key: 'job_postingBy', label: 'Posted by', sortable: true },
{ key: 'Remove', sortable: true }
],
filter: null,
jobs: [
{
ID: '',
job_title: '',
job_name: '',
job_location: '',
job_postingURL: '',
job_postingOn: '',
job_postingBy: ''
},
],
}
},
// this method is to get the data from database
async created () {
try {
this.jobs = await JobService.getJobs();
} catch(err) {
this.error = err.message;
}
},
methods: {
deleteJob (ID) {
axios.delete(`http://localhost:5000/api/jobs/${this.ID}`)
.then((res) => {
this.ID = ''
this.job_title = ''
this.job_name = ''
this.job_location = ''
this.job_postingURL =''
this.job_postingOn = ''
this.job_postingBy = ''
console.log(res)
})
.catch((err) => {
console.log(err)
})
}
}
}
</script>
解决方案
由于您jobs
喜欢一个不能jobs
用作slot-scope
值的数据对象属性,请尝试类似的方法row
,在这种情况下,row
对象包含一些属性,例如item
包含有关此行中显示的当前项目的数据,因此您应该这样做:
<template slot="Remove" slot-scope="row">
<b-btn variant="danger" @click="deleteJob(row.item.ID)"> Delete </b-btn>
</template>
并在您的方法中:
deleteJob (ID) {
axios.delete('http://localhost:5000/api/jobs/'+ID)
.then((res) => {...
推荐阅读
- for-loop - Shopify 循环我想在其他循环下使用循环
- node.js - 由于节点 js 中的 MIME(text/html),我无法包含 js 文件
- python - 使用python从pandas数据框列中删除非法文件名字符
- security - 需要 Jenkins 中 HTTP 响应代码的自定义错误页面
- vuex - 模块化 VueX 结构,[vuex] 未知 getter:
- r - tidyverse 语言中的 map 函数
- reactjs - 在 ag-Grid 中的列标题上添加自定义下拉菜单
- firebase - 如何使用 firebase(Firestore 和 Storage)生成包含 1000 多张图像的 word 文档(docx)?
- python-3.x - Selenium 处理弹出窗口
- java - Gradle 覆盖 zip 存档的默认“artifactId”