类型错误:context.getters.Function不是Vuex的函数

时间:2018-07-13 08:09:15

标签: javascript vue.js vuex

我已经尝试了很多方法来解决此问题。为什么说这个吸气剂功能不存在

action.js

//Module's Actions Functions

export const services_addService = (context, payload) => {
    payload.person = context.getters.contacts_getPersonById(payload.id).person
    let dataDB = context.getters.services_getSort(0);
    let currentDB = dataDB.filter(element => element.person == payload.person);
    let oldDB = context.getters.services_getSort(1).filter(element => element.person == payload.person)
    let getCurrentworker = dataDB.find(element => element.person == payload.person && (element.start == payload.start || element.end == payload.end))

    if(currentDB.length == 0){
        if(getCurrentworker.length == 0){
            commit('addService', payload)
        }
        else {
            commit('replaceService', {key: dataDB.indexOf(getCurrentworker) , data: payload})
        }
    }
    else {
        commit('replaceService', {key: oldDB.indexOf(getCurrentworker), data: payload})
    }
};

getters.js

//Module's Getters Functions

/** Get Contacts */
export const contacts_getData = state => state.contacts.sort(function(a, b){return a.pos - b.pos});

/** Get Contacts Length */
export const contacts_getLength = state => state.contacts.length

/** Get Contact by ID  */
export const contacts_getPersonById = (state,getters,id) => {
    return state.contacts.find(element => {            
        element.id == id
    })
}

我尝试了某些吸气剂参数,例如

export const contacts_getPersonById = state => id => {
        return state.contacts.find(element => {            
            element.id == id
        })
}

这是我在Firefox中的错误

TypeError: context.getters.contacts_getPersonById is not a function[Learn More]

1 个答案:

答案 0 :(得分:0)

您传递给state.contacts.find的测试函数应返回一个布尔值。您只是在比较element.id == id而未返回任何内容。

因此将其更改为:

export const contacts_getPersonById = state => id => {
  return state.contacts.find(element => {
    return element.id == id;
  });

};

或者只是

export const contacts_getPersonById = state => id => {
  return state.contacts.find(element => element.id == id);
};