如何在猫鼬模型中查询嵌套数据 [英] How to query nested data in mongoose model

查看:65
本文介绍了如何在猫鼬模型中查询嵌套数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MEVN堆栈后端和Vuex构建Vue.js应用程序.我正在使用GET请求配置我的Vuex动作处理程序,该请求会提示相应的Express GET路由以查询嵌套在Mongoose中的数据.

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.

用户名作为参数传递给处理程序,并作为参数附加到GET请求URL:

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)
    }
  }

相应的Express路由查询activeUser.name,代表猫鼬模型中的嵌套数据:

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);
    }
  });
});

下面是我的Mongoose模型,其中activeUser.name代表Express路由查询的嵌套数据:

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);

即使使用此设置,GET路由也似乎不会将响应发送回操作处理程序.我认为在快速路由中添加username = req.query.activeUser.name是查询Mongoose中嵌套数据的正确方法,但显然不是.关于如何配置上述Express路由以便查询Mongoose模型中的嵌套数据的任何建议?谢谢!

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!

推荐答案

name位于activeuser内部,因此您需要像这样构造params对象变量:

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);
    }
  });
});

请注意,我还使用activeUserName作为查询参数,如下所示:/currentuser?activeUserName=JS_is_awesome18

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

这篇关于如何在猫鼬模型中查询嵌套数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆