我正在尝试构建一个Vue.js应用程序,它综合了AWS、MongoDB和Express的属性。我使用aws-amplify和aws-amplify-vue为应用程序构建了一个身份验证页面。登录到应用程序后,包含登录AWS用户用户名的元数据将传递给数据对象属性this.name,如下所示:
async beforeCreate() {
let name = await Auth.currentAuthenticatedUser()
this.name = name.username
}然后通过Axios将this.name添加到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;
});
},但是,此方法返回所有用户的数据。我希望将此方法重新配置为只返回.currentAuthenticatedUser()的数据。在我以前使用Firebase的经验中,我会使用如下内容来设置我的.getData()方法:
let ref = db.collection('users')
let snapshot = await ref.where('user_id', '==', firebase.auth().currentUser.uid).get()...in命令在集合中的“user_id”与当前登录的Firebase用户匹配的条件下返回currentUser信息。
为了用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...but中的登录用户来返回当前的用户数据--我知道这可能行不通,因为.where()方法可能是Firebase特有的。关于如何将此.getData()配置为只返回与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;发布于 2019-11-22 22:34:15
不可能将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
});
})您应该做的是通过username获取req.query查询参数,并将其添加到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);
}
});
});https://stackoverflow.com/questions/59001855
复制相似问题