我正在验证一些值:
$collectionConstraint = new Collection(array(
'email' => array(
new NotBlank(),
new Email(),
),
'password' => array(
new NotBlank(),
new MinLength(array('limit' => 6)),
new MaxLength(array('limit' => 25)),
),
));
$data = array('email' => $this->getRequest()->get('email'), 'password' => $this->getRequest()->get('password'));
$errors = $this->get('validator')->validateValue($data, $collectionConstraint);
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,字段(propertyPath)存储方括号 - 我想了解为什么Sf这样做.我必须手动删除所有看似荒谬的括号,所以我想我在某处遗漏了一些功能.
转储$错误:
Symfony\Component\Validator\ConstraintViolationList Object
(
[violations:protected] => Array
(
[0] => Symfony\Component\Validator\ConstraintViolation Object
(
[messageTemplate:protected] => This value should not be blank
[messageParameters:protected] => Array
(
)
[root:protected] => Array
(
[email] =>
[password] =>
)
[propertyPath:protected] => …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 superagent 执行 API 调用,但它对我的 api 密钥进行编码,但被拒绝。
get(url).query({ key: 'Fmjtd%7Cluu').end(function(err, res) {
Run Code Online (Sandbox Code Playgroud)
密钥发送为
Fmjtd%257Cluu
Run Code Online (Sandbox Code Playgroud)
有什么想法如何使用超级代理来防止这种情况吗?如果我将其作为“url”部分的一部分,那就没问题,但如果可能的话,我想将其作为查询数据传递。
我想在我的聚合结果中获取所有文档字段,但是一旦我使用$ group它们就消失了.使用$ project允许我读取我在$ group中定义的任何字段,但是在获取其他字段方面没有运气:
var doc = {
_id: '123',
name: 'Bob',
comments: [],
attendances: [{
answer: 'yes'
}, {
answer: 'no'
}]
}
aggregate({
$unwind: '$attendances'
}, {
$match: {
"attendances.answer": { $ne:"no" }
}
}, {
$group: {
_id: '$_id',
attendances: { $sum: 1 },
comments: { $sum: { $size: { $ifNull: [ "$comments", [] ] }}}
}
}, {
$project: {
comments: 1,
}
}
Run Code Online (Sandbox Code Playgroud)
这导致:
[{
_id: 5317b771b6504bd4a32395be,
comments: 12
},{
_id: 53349213cb41af00009a94d0,
comments: 0
}] …
Run Code Online (Sandbox Code Playgroud) 我目前正在克隆一个对象:
var copy = JSON.parse(JSON.stringify(original));
Run Code Online (Sandbox Code Playgroud)
当我尝试lodash时 - 似乎推荐的方法是cloneDeep(),但这对我来说总是一团糟.我的对象部分由Mongoose查询的结果组成,这可能是造成这种情况的原因.
原版的:
template: 'email/receipt.swig',
templateVars: {
code: '299137819',
Run Code Online (Sandbox Code Playgroud)
用lodash克隆:
template: 'email/receipt.swig',
templateVars: {
'$__': {
strictMode: true,
selected: undefined,
shardval: undefined,
saveError: undefined,
validationError: undefined,
adhocPaths: undefined,
removing: undefined,
inserting: true,
version: undefined,
getters: [Object],
_id: undefined,
populate: undefined,
populated: [Object],
wasPopulated: false,
scope: [Circular],
activePaths: [Object],
ownerDocument: undefined,
fullPath: undefined
},
isNew: false,
errors: undefined,
_maxListeners: 0,
_events: { save: [Object], isNew: [Object] },
_doc: {
code: '299137819'
Run Code Online (Sandbox Code Playgroud)
这里发生了什么?这显然是Mongo的东西,但为什么重新格式化?有没有办法用lodash制作精确的副本?并不是说我当前的方法很痛苦 - 只是试图理解为什么人们说cloneDeep是等价的.
我发布这个问题和答案,希望它能帮助别人(或者如果有更好的答案).
如何以数组形式为Mongoose嵌套模式创建虚拟机?
以下是架构:
var Variation = new Schema({
label: {
type: String
}
});
var Product = new Schema({
title: {
type: String
}
variations: {
type: [Variation]
}
});
Run Code Online (Sandbox Code Playgroud)
我想要一个虚拟的variations
.似乎如果子doc不是数组,那么我们可以简单地这样做:
Product.virtual('variations.name')...
Run Code Online (Sandbox Code Playgroud)
但这只适用于非数组.
我有一个返回承诺的服务.我想检查返回的值是否为空对象.我怎样才能做到这一点.我想我需要以某种方式从promise对象中提取返回的值.
这是我的资源:
app.factory('Auth', ['$resource',
function($resource) {
return $resource('/user/identify', {}, {
identify: {
url: '/user/identify'
},
});
}
]);
Run Code Online (Sandbox Code Playgroud)
然后在服务中:
Auth.identify(function(data) {
// This always passes since data contains promise propeties
if (!_.isEmpty(data)) {
}
});
Run Code Online (Sandbox Code Playgroud)
控制台数据日志给出:
Resource
$promise: Object
$resolved: true
__proto__: Resource
Run Code Online (Sandbox Code Playgroud)
当对象不为空但我想要更通用的方法时,我可以检查预期的属性.
我已经承诺 Mongoose 和 Bluebird - 一切似乎都工作正常,除了返回的文档saveAsync()
只是普通对象并且缺少方法/虚拟。
order.saveAsync().then(function(order) {
order.save(); // errors with object has no method 'save'
Run Code Online (Sandbox Code Playgroud)
我使用“保存”作为示例,但似乎没有内置方法或我的自定义方法可用。
我怎样才能得到一个从返回的 Mongoose 对象saveAsync
?
我有一个输入字段和一个按钮,用户输入一个地址进行地理定位.它是更大形式的一部分,但我希望在按下"Map it"按钮而不是最终表单提交时处理该字段的有效性.如何将此字段设置为无效?调用$setValidity
结果是未定义/不是函数错误.
表格位:
<form name="myForm">
<input type="text" name="addy" ng-model="addy">
<a ng-click="geoCode()">Map it!</a>
<span class="error" ng-hide="myForm.addy.$invalid">Bad field</span>
....
</form>
Run Code Online (Sandbox Code Playgroud)
控制器:
$scope.addy = null
// This is a more complex async function trimmed down to essentials
$scope.geoCode = function() {
$scope.addy.$setValidity('unique', false);
});
Run Code Online (Sandbox Code Playgroud)
我也希望只检查一次这个字段.因此,如果他们输入一个地址并且没有按下Map it按钮,那么它将在提交时被检查,但是如果他们确实按了Map按钮并且字段有效则它将不会再次通过异步功能.关于如何最好地处理这个的建议?
据我了解,这不起作用(这是一个人为的示例 - 请参阅 RxJS 了解我实际运行的内容):
function Foo() {
this.name = 'Johnny Cash'
}
Foo.prototype.who = () => {
console.log(this.name) // undefined
};
var foo = new Foo();
foo.who()
Run Code Online (Sandbox Code Playgroud)
因为this
没有正确的范围。然而,RxJS 文档上的这个页面(最后 2 个底部示例)使用了它。他们如何运行这段代码?
RxJS页面上的代码是否错误?或者我是否需要运行某种 Babel 插件(我已经尝试通过 babel-require 和 babel-polyfill 运行,效果相同)
如何安装 npm 包的最新可用版本?'@latest' 肯定不会获取最新的 - 我认为这意味着最新的稳定版或其他东西。
我一直在使用 hack 一段时间,因为我似乎找不到任何关于此的信息:
npm i extract-text-webpack-plugin@X
Run Code Online (Sandbox Code Playgroud)
“X”导致它失败并转储所有可能的版本,然后我复制并粘贴正确的版本而不是“X”。有点可笑。
我已经尝试过诸如“最新版本”之类的 3rd 方软件包,但它们都无法获得最新版本。
似乎没有官方这样做。例如在撰写本文时,extract-text-webpack-plugin的最新版本是2.0.0-beta.4。但是这样做:
npm i extract-text-webpack-plugin@latest
Run Code Online (Sandbox Code Playgroud)
将安装“1.0.1”
我可以通过执行查看最新版本
npm info pkg versions --json (without --json it will cut off when there are many versions)
Run Code Online (Sandbox Code Playgroud)
由于缺乏实际工具,我想这将是一些 grep 工作。
node.js ×5
javascript ×3
angularjs ×2
mongoose ×2
bluebird ×1
ecmascript-6 ×1
lodash ×1
mongodb ×1
npm ×1
promise ×1
rxjs ×1
superagent ×1
symfony ×1