Javascript 如何从我的前端服务查询用户?
问题: 我正在尝试通过以下方式查询我的前端angular 2服务中当前登录的用户:Javascript 如何从我的前端服务查询用户?,javascript,mongodb,angular,mongoose,Javascript,Mongodb,Angular,Mongoose,问题: 我正在尝试通过以下方式查询我的前端angular 2服务中当前登录的用户: UserModel.findById(userID, function (err, user) { 似乎这种行为在浏览器中是可能的,我如何从我的服务中查询用户 错误: 运行npm后,使用webpack运行构建,然后启动npm: 这让我在线搜索错误的含义,并有效地发现mongoose不支持来自客户端的查询 代码: 模型/用户 客户服务 检查http模块中的Angular2以发出请求。这是一个GET请求,但我相信
UserModel.findById(userID, function (err, user) {
似乎这种行为在浏览器中是可能的,我如何从我的服务中查询用户
错误:
运行npm后,使用webpack运行构建,然后启动npm:
这让我在线搜索错误的含义,并有效地发现mongoose不支持来自客户端的查询
代码:
模型/用户
客户服务
检查http模块中的Angular2以发出请求。这是一个GET请求,但我相信如果您愿意,您可以提出所有其他请求
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'http-app',
templateUrl: 'your-template.html'
})
class YourComponent {
constructor(http: Http) {
http.get('your-endpoint')
.map(response => response.json())
.subscribe(json => this.json = json);
}
}
您是在尝试在诸如electron之类的环境中运行Angular应用程序,还是在浏览器中运行?如果是后者,那么我建议您实际阅读您引用的问题,因为这在浏览器中不起作用。如果您试图询问如何从浏览器访问MongoDB,请不要这样做。相反,在你最喜欢的搜索引擎中输入这个短语,然后开始阅读结果。请解释为什么要投反对票。如有必要,我愿意修改我的问题:@NeilLunn我知道。这就是为什么我在这里要问一个问题:D我想知道如何从我的服务中查询我的用户,因为我当前的实现无法从浏览器中查询。请注意,这不是我的反对票。但我认为我所说的可以解释为什么有人投了反对票。也就是说,关于“我如何使用浏览器”的问题有很多信息,因为你不是第一个问这个问题的人。@NeilLunn我应该说得更清楚一点,我知道我不能从浏览器查询MongoDB,我只是问我能做些什么来从我的服务中查询我的用户。我正在一个服务中使用它,不是组件:即使在服务中,您仍然可以使用相同的http.getendpoint方法
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var mongooseUniqueValidator = require('mongoose-unique-validator');
var schema = new Schema({
firstName: {type: String, required: true},
lastName: {type: String, required: true},
password: {type: String, required: true},
email: {type: String, required: true, unique: true},
polls: [{type: Schema.Types.ObjectId, ref: 'Poll'}],
votes: [{
poll: {type: Schema.Types.ObjectId, ref: 'Poll'},
choice: {type: Number},
}],
});
schema.plugin(mongooseUniqueValidator);
module.exports = mongoose.model('User', schema);
var UserModel = require('../../../models/user');
voted(poll: Poll, userID: string, choice: number) {
UserModel.findById(userID, function (err, user) {
var result = "";
for (var i = 0; i < user.votes.length; i ++) {
if (user.votes[i].poll == poll.pollId) {
result = "disabled";
if (user.votes[i].choice == choice) {
result = "selected";
}
}
}
return result;
})
}
import { Component } from '@angular/core';
import { Http } from '@angular/http';
@Component({
selector: 'http-app',
templateUrl: 'your-template.html'
})
class YourComponent {
constructor(http: Http) {
http.get('your-endpoint')
.map(response => response.json())
.subscribe(json => this.json = json);
}
}