0

I've been looking for quite a while but being a novice I can't find an answer. I would like to filter my array with the id of a property I think is the wrong syntax. Thanks for your help

components

export default {
  props: ["user", "recette"],
  data() {
    return { email: this.$route.params.email };
  },
  components: {},
  methods: {},
  computed: {
    filteredItems: function () {
      return this.recette.filter((recettes) => {
        return recettes.cat_recetteId === 1;
      });
    },
  },
};

VIEW

<template>
  <div>
    <myrecette :recette="recette"/>
    <myfooter />
  </div>
</template>

<script>
import myrecette from "../components/recette";
import myfooter from "../components/myfooter";

export default {
  name: "",
  data() {
    return {
      recette: "",
      user: "",
    };
  },
  components: {
    myrecette,
    myfooter,
  },
  created: function() {
    this.axios.get("http://localhost:3000/recette/all_recette/").then((res) => {
      (this.recette = res.data.recette),
        this.axios
          .get(
            "http://localhost:3000/user/rec_user/" + this.$route.params.email
          )
          .then((res) => {
            this.user = res.data.user;
          });
    });
  },
};
</script>
<style scoped></style>

NODE

router.get("/all_recette", (req, res) => {
    db.recette
        .findAll({
            include: { all: true },
        })
        .then((recette) => {
            if (recette) {
                res.status(200).json({
                    recette: recette,
                });
            } else {
                res.json("il n'y a pas de recettes");

            }
        })
        .catch(err => {
            res.json(err);
        });
});

Here is my complete code as well as my node route.

My error return is

vue.runtime.esm.js?2b0e:619 [Vue warn]: Error in render: "TypeError: this.recette.filter is not a function"

1
  • 2
    please share your whole script Commented Dec 12, 2020 at 19:28

2 Answers 2

1

The filter works by keeping items which return true, so if you want all items having a cat_recetteId of 1, you would change it to:

computed: {
  filteredItems: function() {
    if (!this.recette) return [];
    return this.recette.filter((recettes) => {
      return recettes.cat_recetteId === 1;
    });
  },
},

It's also good practice to use an arrow function in most cases inside of a computed.

Sign up to request clarification or add additional context in comments.

Comments

1

Your filter callback function should return true or false. You're 1) not returning anything and 2) assigning a value (=) instead of doing a comparison (==/===).

computed: {
    filteredItems: function() {
      return this.recette.filter(function(recettes) {
        return recettes.cat_recetteId === 1;
      });
    },
  },

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.