在一个 尝试失败 学习练习让验证者使用'document.update',我遇到了一些我不理解的东西.
我现在知道它不起作用,但我尝试过的一件事就是将我的选项设置为{runValidators:true,context:'query'}.在我的验证器函数中,我尝试了console.logging(this),有和没有上下文:"query"选项.
没有区别.我收到一个大对象(这被称为'查询对象'?)这似乎违背了我在这里读到的内容.
在上面的颜色验证功能中,这是指在使用文档验证时验证的文档.但是,在运行更新验证程序时,正在更新的文档可能不在服务器的内存中,因此默认情况下不会定义此值.
即使没有上下文选项,它也没有未定义.
我甚至尝试使它成为一个箭头函数,看看这个词汇是否有所不同.在这种情况下,这是未定义的,但同样,更改上下文选项并没有什么不同.(我还在学习,所以我不知道那部分是否相关).
在模型中:
let Property = mongoose.model('Property', {
name: {type:String, required:true},
occupancy: {type:String},
maxTenants: Number,
tenants: [{ type:mongoose.Schema.Types.ObjectId, ref: 'Tenant', validate: [checkMaxTenants, "Maximum tenants exceeded for this property. Tenant not added."]}]
});
function checkMaxTenants(val){
console.log("this",this);
// return this.tenants.length <= this.maxTenants;
return true;
}
Run Code Online (Sandbox Code Playgroud)
并在路线:
property.update({$set: {tenants:property.tenants}},{new:true,runValidators:true,context:'query'}, function(err,savedProperty){
Run Code Online (Sandbox Code Playgroud)
有什么可以帮助我更好地理解我认为我正在阅读的东西和我看到的东西之间的差异会很棒!
使用打开 noImplicitAny 的Typescript Playground ,我输入以下代码:
async function a () {
const b = JSON.parse('{"a":"x"}');
console.log(b)
}
Run Code Online (Sandbox Code Playgroud)
如果我将鼠标悬停在第二行的“b”上,我会看到它的类型被推断为any。然而,没有错误。我是否误解了 noImplicitAny 应该做什么或者这是一个错误?
我有三个盒子,我想将各种组合放入一个容器中.盒子有时会以组合形式出现,有时则不然.
如果所有三个盒子都放在容器中,顶部应该有一个全宽的盒子,下面有一个两个半宽的盒子.
如果容器中有两个盒子,那么应该只有两个半宽盒子.
如果有一个盒子,它应该是全宽的.
我想出了一个使用wrap:wrap-reverse的解决方案,但它要求你在html中以相反的顺序放置元素.轻微的烦恼.
所以我使用了flex-direction:row-reverse和order来反转内容的顺序.
我对这个解决方案有一个小问题,因为如果我们想让相同的css处理5个盒子,那么使用order会产生一个不太灵活的解决方案.
现在我倾向于不使用顺序而只是生活在颠倒的HTML中.你怎么看?我在某个地方错过了一颗银弹吗?
https://jsfiddle.net/uesje5dq/
.container {
display: flex;
flex-wrap: wrap-reverse;
flex-direction: row-reverse;
}
.item {
flex: 1;
flex-basis: 50%;
}
.item:nth-child(1) {
order: 3;
}
.item:nth-child(2) {
order: 2;
}
.item:nth-child(3) {
order: 1;
}
Run Code Online (Sandbox Code Playgroud)
================================================== =========================
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div class="container">
<div class="item" style="height: 100px;width:100%;background-color: red;"></div>
<div class="item" style="height: 100px;width:100%;background-color: green;"></div>
<div class="item" style="height: 100px;width:100%;background-color: blue;"></div>
</div>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) css ×1
css3 ×1
flexbox ×1
html ×1
javascript ×1
mongodb ×1
mongoose ×1
node.js ×1
typescript ×1
validation ×1