简体   繁体   English

如何在 Vue.js Express 应用程序中设置路由参数

[英]How to set up route parameters in Vue.js Express app

I am attempting to build a basic Vue.js Express profile interface that returns profile info of a specific user based on a route parameter id associated with each user.我正在尝试构建一个基本的 Vue.js Express 配置文件接口,该接口根据与每个用户关联的路由参数 id 返回特定用户的配置文件信息。 The .get() request in Vue.js is set up as the following: Vue.js 中的.get()请求设置如下:

  created () {
    let uri = `http://localhost:3000/users/${this.$route.params.id}`;
    this.axios.get(uri).then((response) => {
      this.profile = response.data;
    });
  },

The corresponding GET route in Express.js is set up as the following: Express.js中对应的GET路由设置如下:

// mongodb
const mongo = require('mongodb').MongoClient;
const url = '...'; // connection url
const usersDB = 'users'; // users db name


app.get('/users/:id', function(req, res) {

let id = req.params.id;

  var users;
  const findUsers = function(db, callback) {
    const collection = db.collection('documents');
    // no query filter
    collection.find({}).sort( {username: 1} )
    .toArray(function(err, docs) {
      users = docs;
      callback(docs);
    });
  }
  mongo.connect(url, function(err, client) {
    // assert.equal(null, err);

    const db = client.db(usersDB);
    findUsers(db, function() {
      // send users
      res.status(200).send(users);
      client.close();
    });
  });
});

In the above Express route, I added let id = req.params.id with the intention of prompting this route to respond with specific user info based on req.params.id .在上面的 Express 路由中,我添加了let id = req.params.id ,目的是提示该路由根据req.params.id使用特定的用户信息进行req.params.id I am not sure how to further configure this route to actually return such info based on id.我不确定如何进一步配置此路由以根据 id 实际返回此类信息。 I tried implementing the following in the route:我尝试在路线中实现以下内容:

collection.find({_id: mongo.ObjectId(req.params.id)})

instead of using:而不是使用:

collection.find({}).sort( {username: 1} )

...but that did not work. ......但这没有用。 Any idea how to set up this route to return data based on req.params.id ?知道如何设置此路由以根据req.params.id返回数据吗? Thanks!谢谢!

UPDATED EXPRESS ROUTE更新的快速路线

// get all users
app.get('/users/:id', function(req, res) {

  var o_id = new mongo.ObjectID(req.params.id))
  // get all users
  var users;
  const findUsers = function(db, callback) {
    const collection = db.collection('documents');
    // no query filter
    collection.find({'_id': o_id})
    .toArray(function(err, docs) {
      users = docs;
      callback(docs);
    });
  }
  mongo.connect(url, function(err, client) {
    const db = client.db(usersDB);
    findUsers(db, function() {
      // send users
      res.status(200).send(users);
      client.close();
    });
  });
});

It seems the results aren't returned because of ObjectId comparision not working.由于 ObjectId 比较不起作用,似乎没有返回结果。 Creating new ObjectID using the id from req.params and then doing collection.find should bring back the results使用 req.params 中的 id 创建新的 ObjectID 然后执行 collection.find 应该带回结果

var o_id = new mongo.ObjectID(req.params.id);)
collection.find({'_id': o_id});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM