首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从我的角度前端服务中查询用户?

如何从我的角度前端服务中查询用户?
EN

Stack Overflow用户
提问于 2017-05-19 09:52:08
回答 1查看 51关注 0票数 0

问题:

我试图通过以下方式查询前端角2服务中当前登录的用户:

代码语言:javascript
复制
 UserModel.findById(userID, function (err, user) {

似乎这种行为在浏览器中是可能的,我如何从我的服务中查询用户呢?

错误:

运行npm run build之后,使用webpack,然后运行npm start

代码语言:javascript
复制
user.js?b9cb:19 Uncaught TypeError: mongoose.model is not a function

这让我在网上搜索错误的意义,有效地发现猫鼬不支持客户端的查询?

https://github.com/Automattic/mongoose/issues/4779

代码:

模型/用户

代码语言:javascript
复制
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

代码语言:javascript
复制
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;
          })
        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-05-19 10:17:54

检查Angular2的http模块以发出请求。这是一个GET请求,但我相信如果您愿意的话,您可以提出所有其他的请求。

代码语言:javascript
复制
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);
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44066842

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档