[英]How can I query a User from my Angular front-end service?
题:
我正在尝试使用以下命令在前端angular 2服务中查询当前登录的用户:
UserModel.findById(userID, function (err, user) {
从浏览器看来,这种行为是可能的,如何从我的服务中查询用户?
错误:
运行npm run build
后,使用webpack npm run build
,然后再使用npm start
:
user.js?b9cb:19 Uncaught TypeError: mongoose.model is not a function
是哪个使我能够在线搜索错误的含义并有效地发现猫鼬不支持来自客户端的查询?
https://github.com/Automattic/mongoose/issues/4779
码:
型号/用户
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;
})
}
检查Angular2的http模块以发出请求。 这是一个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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.