我想访问存在于守卫中的上下文对象,在我的承载策略验证方法中。我可以将它作为参数与令牌一起传递吗?
Bearer-auth.guard.ts:
@Injectable()
export class BearerAuthGuard extends AuthGuard('bearer') {
canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
return super.canActivate(context);
}
}
Run Code Online (Sandbox Code Playgroud)
http.strategy.ts:
@Injectable()
export class HttpStrategy extends PassportStrategy(Strategy) {
constructor(private globalService: GlobalService) {
super();
}
async validate(token: string) {
const customer = await this.globalService.validateCustomer(token);
if (!customer) {
throw new UnauthorizedException();
}
return customer;
}
}
Run Code Online (Sandbox Code Playgroud)
我想要这样的东西:
async validate(token: string, context) { // <-- context object as argument
const customer = await this.globalService.validateCustomer(token);
if (!customer) {
throw new UnauthorizedException();
}
return …Run Code Online (Sandbox Code Playgroud) 我有多个要连接的数据库。但只有其中之一具有静态名称。例如,该数据库的名称是stores,并且我有一个指示其他数据库名称的表。现在我想在调用第一条路由后更改连接提供程序。我怎样才能做到这一点?
我尝试使用动态模块,但我不知道如何使用它。
我在点击事件中有这个代码
let browser = new InAppBrowser('http://example.com/test', '_self');
browser.executeScript({code: "(function() { alert(123); })()"});
Run Code Online (Sandbox Code Playgroud)
当我单击按钮时,inAppBrowser会打开页面,但脚本不会运行.
我关闭了浏览器,然后再次单击该按钮,现在脚本运行了.
为什么它没有第一次运行?
我使用此代码获取 php 中的特定字段
$client = new MongoDB\Client("mongodb://localhost:27017");
$collection = $client->test->users;
$result = $collection->find(array(), array('name' => 1, '_id' => 1));
Run Code Online (Sandbox Code Playgroud)
但它返回所有字段。
我从这个链接得到最后一行:http :
//php.net/manual/en/mongo.sqltomongo.php
数据:
object(MongoDB\Model\BSONDocument)#36 (1) { ["storage":"ArrayObject":private]=> array(7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#35 (1) { ["oid"]=> string(24) "598ebdeab318de646ca08788" } ["is_hidden"]=> bool(false) ["priority"]=> int(1) ["picture"]=> string(21) "15025269499885875.jpg" ["__v"]=> int(0) ["name"]=> string(8) "John" } }
Run Code Online (Sandbox Code Playgroud)
预期结果:
object(MongoDB\Model\BSONDocument)#36 (1) { ["storage":"ArrayObject":private]=> array(7) { ["_id"]=> object(MongoDB\BSON\ObjectID)#35 (1) { ["oid"]=> string(24) "598ebdeab318de646ca08788" } ["name"]=> string(8) "John" } }
Run Code Online (Sandbox Code Playgroud) 我在我的快递申请中有这个代码
var app = require('express')()
var bodyParser = require('body-parser')
var cors = require('cors')
app.use(bodyParser.urlencoded({ extended: true }))
app.post('/user', cors(), function (req, res) {
res.send(req.body.username);
})
app.listen(3000, function () {
console.log('Example app listening on port 3000!')
})
Run Code Online (Sandbox Code Playgroud)
这是我发送请求的角度2函数
getData() {
let headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
let params = 'username=test';
this.http.post('http://localhost:3000/user', params, {headers: headers})
.map(res => res.json())
.subscribe(data => {});
}
Run Code Online (Sandbox Code Playgroud)
我在控制台中收到此错误:
XMLHttpRequest无法加载http:// localhost:3000/user.对预检请求的响应未通过访问控制检查:请求的资源上不存在"Access-Control-Allow-Origin"标头.因此不允许来源' http:// localhost:4200 '访问.
但是当我使用jquery ajax发送请求时,它的工作没有任何错误.
我阅读了很多有关在MongoDB中嵌入的内容,但是我仍然不知道何时使用它。我想到了一些方案:
我有一个名为UserGroups的集合,具有以下字段:
现在,我想向“用户”集合添加一个用户:
我应该在Users集合中有一个groupName字段,并在UserGroups集合得到更新时对其进行更新。
我应该在Users集合中添加groupId字段,并在需要UserGroups.name时执行连接。
我应该将用户文档作为嵌入式文档添加到UserGroups集合中。
或者,我应该将userGroup文档作为嵌入式文档添加到Users集合中。不过,我认为我不应该这样做。
我应该使用哪个选项?
mongodb ×2
nestjs ×2
node.js ×2
typescript ×2
ajax ×1
angular ×1
cordova ×1
express ×1
inappbrowser ×1
ionic2 ×1
javascript ×1
jquery ×1
passport.js ×1
php ×1
sequelize.js ×1