编辑:我发现console.log(this)在setPassword方法内部运行只返回哈希和盐.我不确定为什么会发生这种情况,但是它表明它this并没有像它应该那样引用模型.
我有以下模式与以下实例方法:
let userSchema = new mongoose.Schema({
username: {type: String, required: true},
email: {type: String, required: true, index: {unique: true}},
joinDate: {type: Date, default: Date.now},
clips: [clipSchema],
hash: {type: String},
salt: {type: String}
})
userSchema.methods.setPassword = (password) => {
this.salt = crypto.randomBytes(32).toString('hex')
this.hash = crypto.pbkdf2Sync(password, this.salt, 100000, 512, 'sha512').toString('hex')
}
Run Code Online (Sandbox Code Playgroud)
在这里调用实例方法,然后保存用户:
let user = new User()
user.username = req.body.username
user.email = req.body.email
user.setPassword(req.body.password)
user.save((err) => {
if (err) {
sendJsonResponse(res, 404, err)
} …Run Code Online (Sandbox Code Playgroud) loadAdList$是一个观察actions$流的Observable :
loadAdList$: Observable<Action> = this.actions$
.ofType<adActions.Load>(adActions.LOAD)
.switchMap((action) => {
return Observable.fromPromise(store.findAll('ad', action.payload)
.then((ads) => {
return new adActions.LoadSuccess(ads);
})
.catch((err) => {
return new adActions.LoadFail(err);
}));
});
Run Code Online (Sandbox Code Playgroud)
它在浏览器中工作,没有问题.但是,我也希望对它进行单元测试:
actions$ = hot('-a', { a: loadAction });
const storeResponse = Promise.resolve(mockAds);
const expected$ = cold('-c', { c: loadSuccessAction });
spyOn(store, 'findAll').and.returnValue(storeResponse);
expect(effects.loadAdList$).toBeObservable(expected$);
Run Code Online (Sandbox Code Playgroud)
测试失败,具体如下:
Expected
to deep equal
{"frame":10,"notification":{"kind":"N","value":{"payload":"[
...
Run Code Online (Sandbox Code Playgroud)
我认为这个问题与store.findAll返回承诺的方法有关.这是基于以下测试的结果:
.switchMap((action) => {
// This test will pass
return Observable.from([new adActions.LoadSuccess(mockAds)]);
// This test will fail …Run Code Online (Sandbox Code Playgroud) 最近我在我的代码中做了以下事情:
for(auto& item : something.getSomeVector())
Run Code Online (Sandbox Code Playgroud)
当我开始我的优化传递时,我开始怀疑这是否比以下内容更低效:
std::vector<Type> vTypes = something.getSomeVector();
for(auto& item : vTypes)
Run Code Online (Sandbox Code Playgroud)
将一个for-each循环复制载体或只保留调用该函数?
谢谢!
我在一个字段上有一个异步验证器zip:
zip: ['', {
validators: [
Validators.required,
Validators.minLength(5),
Validators.maxLength(5)
],
asyncValidators: [
adPostFormValidators.isValidZip(this.locationService)
]
},
],
Run Code Online (Sandbox Code Playgroud)
但是,在我单击该字段之外之前,该字段似乎并未反映来自异步验证器的错误。例如,这是在我单击之前(这null是字段的错误状态):
我知道异步验证器已经运行,因为我将其结果输出到控制台:
然后,当我单击或失去焦点时,错误状态现在是准确的:
但是,验证器没有再次运行,因为控制台中没有记录任何新内容。
angular ×1
c++ ×1
c++11 ×1
for-loop ×1
foreach ×1
jasmine ×1
mongodb ×1
mongoose ×1
node.js ×1
observable ×1
optimization ×1
promise ×1
typescript ×1