小编Rez*_*994的帖子

http策略的validate方法中如何访问请求url?

我想访问存在于守卫中的上下文对象,在我的承载策略验证方法中。我可以将它作为参数与令牌一起传递吗?

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)

javascript node.js typescript passport.js nestjs

6
推荐指数
1
解决办法
1154
查看次数

如何动态连接到nestjs中的数据库?

我有多个要连接的数据库。但只有其中之一具有静态名称。例如,该数据库的名称是stores,并且我有一个指示其他数据库名称的表。现在我想在调用第一条路由后更改连接提供程序。我怎样才能做到这一点?

我尝试使用动态模块,但我不知道如何使用它。

sequelize.js typescript nestjs

5
推荐指数
1
解决办法
646
查看次数

如何在离子2中使用inAppBrowser插件的"executeScript"方法?

我在点击事件中有这个代码

let browser = new InAppBrowser('http://example.com/test', '_self');
browser.executeScript({code: "(function() { alert(123); })()"});
Run Code Online (Sandbox Code Playgroud)

当我单击按钮时,inAppBrowser会打开页面,但脚本不会运行.
我关闭了浏览器,然后再次单击该按钮,现在脚本运行了.

为什么它没有第一次运行?

cordova inappbrowser ionic-framework ionic2

4
推荐指数
1
解决办法
8772
查看次数

如何获取 MongoDB PHP 中的特定字段?

我使用此代码获取 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)

php mongodb

4
推荐指数
1
解决办法
3776
查看次数

如何克服从Angular 2到Node/express的CORS?

我在我的快递申请中有这个代码

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发送请求时,它的工作没有任何错误.

ajax jquery node.js express angular

3
推荐指数
1
解决办法
1324
查看次数

我们什么时候应该在MongoDB中使用嵌入式文档?

我阅读了很多有关在MongoDB中嵌入的内容,但是我仍然不知道何时使用它。我想到了一些方案:

我有一个名为UserGroups的集合,具有以下字段:

  • _ID
  • 名称

现在,我想向“用户”集合添加一个用户:

  1. 我应该在Users集合中有一个groupName字段,并在UserGroups集合得到更新时对其进行更新。

  2. 我应该在Users集合中添加groupId字段,并在需要UserGroups.name时执行连接。

  3. 我应该将用户文档作为嵌入式文档添加到UserGroups集合中。

  4. 或者,我应该将userGroup文档作为嵌入式文档添加到Users集合中。不过,我认为我不应该这样做。

我应该使用哪个选项?

mongodb

3
推荐指数
1
解决办法
2954
查看次数