gpt4 book ai didi

javascript - 如何从 Angular 前端服务查询用户?

转载 作者:行者123 更新时间:2023-12-03 04:17:54 27 4
gpt4 key购买 nike

问题:

我正在尝试使用以下方式查询前端 Angular 2 服务中当前登录的用户:

 UserModel.findById(userID, function (err, user) {

浏览器似乎可以实现这种行为,我如何从我的服务中查询用户?

<小时/>

错误:

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

user.js?b9cb:19 Uncaught TypeError: mongoose.model is not a function

这让我上网搜索错误的含义,结果发现 mongoose 不支持客户端查询?

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);
}
}

关于javascript - 如何从 Angular 前端服务查询用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44066842/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com