如何使用 Vue.js、AWS Amplify 和 MongoDB 检索 currentUser 数据

How to retrieve currentUser data with Vue.js, AWS Amplify, and MongoDB

我正在尝试构建一个综合了 AWS、MongoDB 和 Express 属性的 Vue.js 应用程序。我使用 aws-amplifyaws-amplify-vue 为应用程序构建了一个身份验证页面。登录应用程序后,包含登录 AWS 用户用户名的元数据被传递到数据对象 属性 this.name 中,如下所示:

  async beforeCreate() {
    let name = await Auth.currentAuthenticatedUser()
    this.name = name.username
  }

this.name 然后通过 Axios 添加到 MongoDB:

    async addName() {
        let uri = 'http://localhost:4000/messages/add';
        await this.axios.post(uri, {
          name: this.name,
        })
        this.getMessage()
      } 

我还有一个 getName() 方法用于从 MongoDB:

检索数据
    async getData () {
      let uri = 'http://localhost:4000/messages';
      this.axios.get(uri).then(response => {
        this.userData = response.data;
      });
    },

但是,此方法 returns 数据适用于所有用户。我想将此方法重新配置为仅 .currentAuthenticatedUser() 的 return 数据。根据我以前使用 Firebase 的经验,我会使用类似以下内容的 .getData() 方法来设置:

let ref = db.collection('users')
let snapshot = await ref.where('user_id', '==', firebase.auth().currentUser.uid).get()

...为了 return currentUser 信息,条件是集合中的 'user_id' 与当前登录的 Firebase 用户匹配。

为了用 MongoDB 实现这一点,我尝试像这样配置上面的方法:

    async getData () {
      let uri = 'http://localhost:4000/messages';
      let snapshot = await uri.where('name', '==', this.name);
      this.axios.get(snapshot).then(response => {
        this.userData = response.data;
      });
    },

我的想法是通过比较 MongoDB 集合中的 'name' 与存储在 this.name 中的登录用户来尝试 return 当前用户数据。 .但我知道这可能行不通,因为 .where() 方法可能是 Firebase 独有的。关于如何将此 .getData() 配置为仅 return 与 currentAuthenticatedUser 关联的数据的任何建议?谢谢!

快速路线:

const express = require('express');
const postRoutes = express.Router();

// Require Post model in our routes module
let Post = require('./post.model');

// Defined store route
postRoutes.route('/add').post(function (req, res) {
  let post = new Post(req.body);
  post.save()
    .then(() => {
      res.status(200).json({'business': 'business in added successfully'});
    })
    .catch(() => {
      res.status(400).send("unable to save to database");
    });
});

// Defined get data(index or listing) route
postRoutes.route('/').get(function (req, res) {
    Post.find(function(err, posts){
    if(err){
      res.json(err);
    }
    else {
      res.json(posts);
    }
  });
});

module.exports = postRoutes;

无法将 where 子句应用于 uri AFAIK。您应该做的是向您在后端进行的实际查询添加一个 where 子句,为此,发送您想要通过查询参数过滤查询的 username ,如下所示: /messages?name=JohnDoe

所以基本上,如果您按照您的建议使用 Node/Express 后端,并使用 Mongoose 作为 MongoDB 的 ODM,您的请求可能看起来像这样:

const Users = require('../models/users.model');

Users.find({}, function (e, users) {
    if (e) {
        return res.status(500).json({
            'error': e
        })
    }

    res.status(200).json({
        'data': users
    });
})

你应该做的是通过req.query获取username查询参数,并将其添加到find函数第一个参数的选项中。

const Users = require('../models/users.model');

let params = {},
    name = req.query.name;

if (name) {
    params.name = name
}

Users.find(params, function (e, users) {
    if (e) {
        return res.status(500).json({
            'error': e
        })
    }

    res.status(200).json({
        'data': users.slice
    });
})

这样,如果您指向 /messages?name=John,您将获得名称为 "John" 的用户。

编辑:

如果你的后台是这样配置的

postRoutes.route('/').get(function (req, res) {
    Post.find(function(err, posts){
    if(err){
      res.json(err);
    }
    else {
      res.json(posts);
    }
  });
});

您应该做的是从 get 方法中获取查询参数

postRoutes.route('/').get(function (req, res) {
    let params = {},
        name = req.query.name

    if (name) {
       params.name = name
    }

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