1

I am attempting to build a Vue.js app with a MEVN stack backend and Vuex. I am configuring my Vuex action handler with a GET request that prompts a corresponding Express GET route to query data nested in Mongoose.

A username is passed into the handler as an argument and appended to the GET request URL as a parameter:

  actions: {
    loadPosts: async (context, username) => {
      console.log(username)
      let uri = `http://localhost:4000/posts/currentuser?username=${username}`;
      const response = await axios.get(uri)
      context.commit('setPosts', response.data)
    }
  }

The corresponding Express route queries activeUser.name, which represents the nested data in the Mongoose Model:

postRoutes.route('/currentuser').get(function (req, res) {
  let params = {},
    username = req.query.activeUser.name
    if (username) {
       params.username = username
    }
    Post.find(params, function(err, posts){
    if(err){
      res.json(err);
    }
    else {
      res.json(posts);
    }
  });
});

Below is my Mongoose model, with activeUser.name representing the nested data queried by the Express route:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

let Post = new Schema({
  title: {
    type: String
  },
  body: {
    type: String,
  },
  activeUser: {
    name: {
      type: String
    }
  }
},{
    collection: 'posts'
});

module.exports = mongoose.model('Post', Post);

Even with this setup, the GET route does not appear to send a response back to the action handler. I thought adding username = req.query.activeUser.name in the express route would be the right method for querying the nested data in Mongoose, but apparently not. Any recommendations on how to configure the above Express route in order to query the nested data in the Mongoose model? Thanks!

7
  • what is the output posts Commented Feb 7, 2020 at 6:41
  • Manjeet Thakur, could you clarify what you are asking? Commented Feb 7, 2020 at 6:43
  • Post.find(params, function(err, posts){ console.log(err, posts)} Commented Feb 7, 2020 at 6:44
  • I tried console.log there, but got no output. The server is returning an error: Cannot read property 'name' of undefined Commented Feb 7, 2020 at 6:47
  • change req.query.activeUser.name to req.query.activeUser.username Commented Feb 7, 2020 at 6:49

1 Answer 1

1

name is inside activeuser so you need to construct params object variable like this:

postRoutes.route("/currentuser").get(function(req, res) {
  let params = {
    activeUser: {}
  };

  let username = req.query.activeUserName;

  if (username) {
    params.activeUser.name = username;
  }

  Post.find(params, function(err, posts) {
    if (err) {
      res.json(err);
    } else {
      res.json(posts);
    }
  });
});

Note that I also used activeUserName as query param like this: /currentuser?activeUserName=JS_is_awesome18

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

1 Comment

Your answer also greatly improved my understanding of how querying in an Express route works. Much appreciated.

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.