问题:
我试图通过以下方式查询前端角2服务中当前登录的用户:
UserModel.findById(userID, function (err, user) {似乎这种行为在浏览器中是可能的,我如何从我的服务中查询用户呢?
错误:
运行npm run build之后,使用webpack,然后运行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);client.service
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;
})
}发布于 2017-05-19 10:17:54
检查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);
}
}https://stackoverflow.com/questions/44066842
复制相似问题