当我在没有指定 的情况下设置表单时enctype,Firefox 会自动将其设置为application/x-www-form-urlencoded并req.body包含输入到表单中的所有参数的漂亮的 JSON 表示。但是当我将 enctype 更改multipart/form-data req.body为突然为空时。
这是我的表格:
<form action="/create" method="post" enctype="multipart/form-data">
<fieldset>
<div>
<label>Category:</label>
</div>
<div>
<select name="category">
<option value="standard">Standard</option>
<option value="custom">Custom</option>
</div>
<div>
<input type="text" name="description">
</div>
<div>
<label>User ID:</label>
</div>
<div>
<input type="text" name="userid">
</div>
<div>
<input type="submit" value="Go">
</div>
</fieldset>
</form>
Run Code Online (Sandbox Code Playgroud)
console.log(JSON.stringify(req.body, null, 2));当enctypeismultipart/form-data和 whenenctype未指定时,执行 a 会打印出一个空对象,它会打印出如下内容:
{
category: "standard",
userid: "foo"
}
Run Code Online (Sandbox Code Playgroud)
发生这种情况的任何原因?
我遇到了与此相同的问题,但提供的解决方案对我不起作用.
我从Symfony下载页面下载了带有供应商软件包的Symfony 2.4.1标准供应商.解压缩文件并下载composer.phar后,我运行php composer.phar install并收到此错误:
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Your requirements could not be resolved to an installable set of packages.
Problem 1
- symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, make sure to have the extension providing it.
- symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library …Run Code Online (Sandbox Code Playgroud) 我正在读的书说,当您的类包含一个引用或const成员时,使用编译器生成的复制构造函数或赋值运算符将不起作用。例如,
#include <iostream>
#include <string>
using namespace std;
class TextBlock
{
public:
TextBlock (string str) : s(str) {
cout << "Constructor is being called" << endl;
}
string& s;
};
int main () {
TextBlock p("foo");
TextBlock q(p);
q = p;
cout << "Q's s is " << q.s << endl;
return(0);
}
Run Code Online (Sandbox Code Playgroud)
根据我的书,这两行TextBlock q(p);和q = p;都应返回编译器错误。但是使用Linux的g ++编译器时,我只会看到一行错误。q = p;当我将其注释掉时,这可以正常工作并且代码可以编译。正确的s输出给Q,因此显然是由编译器生成的副本构造函数复制的。将行更改为时string& s;,我得到相同的结果const string s。
C ++是否进行了一些更改,现在允许自动为引用和const对象生成复制构造函数,但不允许赋值运算符生成?还是我只是不正确地理解这本书?有什么想法吗?
c++ compiler-construction copy-constructor assignment-operator
我将 Typescript 与 Angular2 结合使用,就像 Angular2 英雄之旅教程中一样。
我有一个输入字段想要附加一个change事件,以便在字段更改时可以执行一些自定义逻辑。我需要知道该字段的当前值来执行逻辑,因此我不想将该字段与 绑定ngModel,因为这将覆盖该属性,然后我才能检索其更改前的先前值。
所以我有类似的东西:
<input (change)="handleChange(myObj, $event)" value={{ myObj.myField }}... />
Run Code Online (Sandbox Code Playgroud)
然后在handleChange中:
handleChange (obj: MyObjectClass, e: Event) {
oldValue: number = obj.myField;
newValue : number = parseInt(e.target.value);
// Do some logic
obj.myField = newValue;
}
Run Code Online (Sandbox Code Playgroud)
虽然这在代码中运行良好,但 Typescript 编译器会抛出error TS2339: Property 'value' does not exist on type 'EventTarget'.错误newValue : number = parseInt(e.target.value);
有更好的方法吗?
上下文
我正在编写一个 Angular2 应用程序,我希望在其中使用多个服务来访问不同的端点。我希望每个服务都缓存其数据,以减少发出的请求数量,并允许多个组件在将数据发送到端点之前修改缓存中的数据。
编码
@Injectable()
export class MyService {
private items: any;
constructor (private http: Http) {
this.items = [];
}
getItems () {
if(!this.items) {
return this.http.get(this.endpoint).map((data) => {
this.items = data;
return data;
});
} else {
return Observable.of(this.items);
}
}
addItem (item) {
this.items.push(item);
}
saveItems () {
return this.http.put(endpoint, this.items);
}
}
Run Code Online (Sandbox Code Playgroud)
那么 Component1 可能会这样做:
userClickedAddItem (item) {
this.myService.addItem(item);
}
Run Code Online (Sandbox Code Playgroud)
Component2 可能会这样做:
userClickedSave () {
this.myService.saveItems().subscribe((items) => {
// Do something
});
}
Run Code Online (Sandbox Code Playgroud)
然后在组件的模块中我正在做类似的事情
@NgModule({ …Run Code Online (Sandbox Code Playgroud) 似乎 Angular Router 的所有指南都讨论了重定向到特定路由。但是,如果您想根据某些内部逻辑重定向到不同的路线,会发生什么情况?
例如:
routes: [
{path: 'link-from-email/:confirmationToken', customFunction: () => {
myService.checkTokenStatus(token).subscribe((status) => {
if(status === PREMIUM_ACCOUNT) {
router.navigate('premium-welcome-page');
} else if(status === BASIC_ACCOUNT) {
router.navigate('basic-welcome-page');
} else {
router.navigate('registration-page');
}
});
}]
Run Code Online (Sandbox Code Playgroud)
在 Angular 中执行此操作的标准方法似乎是通过路由重定向到一个组件,该组件根据返回的内容在其模板中显示不同的内容myService。但有些用例不希望将其包含在一个模板中。例如,在上面的示例中,我们可能想要显示三个不同的页面,并且我们可能发现将所有逻辑包含在单个组件中效率不高。或者,我们可能希望将用户引导至两个不同的流程,每个流程都组织到单独的目录中,而不是在一个组件中包含两个页面。
当然,我们可以创建一个带有空模板的虚拟组件来处理这个逻辑。但这似乎效率很低。
有没有办法以某种方式将该逻辑包含在路由中?或者我们总是必须重定向到某个组件?
大多数Angular教程都讨论了使用Protractor进行端到端测试来测试编译模板是否按预期出现.我想知道是否有可能完成单元测试.
大多数关于在单元测试中引用HTML代码的教程都描述了在测试中编译自己编写的代码,例如,以确保正确访问指令:
describe('some function', function () {
it('should do something', inject(function ($compile, $rootScope) {
var element = $compile('<div id = "foo" ng-hide = "somecondition">Bar</div>')($Scope);
$rootScope.digest();
//Search for a given DOM element and perform some test here
}));
});
Run Code Online (Sandbox Code Playgroud)
但是,假设我想测试实际模板文件中的代码.就像我想测试ng-hide是否成功设置一样.我希望能够做到这样的事情:
describe('some function', function () {
it('should do something', function () {
//Get the div with ID 'foo' in the compiled template
var elm = $('#foo');
expect(elm.css('display')).toEqual('none');
});
});
Run Code Online (Sandbox Code Playgroud)
当我这样做时,这不起作用.elm设置为一些HTML/Javascript代码,但不是模板的代码,并elm.css('display')返回未定义.
有没有办法用Jasmine/Angular设置单元测试?
我想在一个页面中验证量角器中的表行数,在该页面中,此类表格不止一次出现.我一直在尝试使用first-of-type选择器,但它似乎正在捕捉两个表,因为它们不会并排显示.
例如,给定此HTML:
<div>
<table class="foo">
<tr>
<th>First row</th>
<th>Second row</th>
<th>Third row</th>
<th>Fourth row</th>
</tr>
</table>
</div>
<div>
<table class="foo">
<tr>
<th>First row</th>
<th>Second row</th>
<th>Third row</th>
<th>Fourth row</th>
</tr>
</table>
</div>
Run Code Online (Sandbox Code Playgroud)
而这个量角器代码:
element.all(by.css('table:first-of-type tr:first-of-type th')).then(function (elements) {
expect(elements.length).toBe(4);
});
Run Code Online (Sandbox Code Playgroud)
量角器失败的原因是有8个元素而不是4个.看起来table:first-of-type选择器捕获了两个元素,因为它们都是它们父div组件的第一个子元素.我的项目结构是这样的,最好不要在每个包装中添加单个类div.
有没有办法获得在Protractor中找到的第一个元素,然后搜索其子元素?
用英语讲
当我尝试require在Node.js shell中加载一个模块来使用它时,我得到了一些意想不到的结果(而不是编写脚本并使用Node运行它).例如,在脚本中,我可以这样做:
var _ = require('grunt');
grunt.registerTask(/* ...*/);
Run Code Online (Sandbox Code Playgroud)
这很好用.但是当我尝试在Node.js shell中执行此操作时,除非我指定node_modules目录,否则它首先找不到该模块,然后调用其中一个模块的方法只能在它停止之前工作一次.
在Tech-Speake中
在当前目录中,我有以下子目录:
- node_modules
- lodash
- grunt
Run Code Online (Sandbox Code Playgroud)
现在我想使用其中一个已安装的库来使用Node.js shell:
$ node
> var _ = require('lodash');
undefined
> _ = require('./node_modules/lodash');
// Long output of function list
Run Code Online (Sandbox Code Playgroud)
现在,如果我尝试使用lodash,它会工作一次然后停止,我必须再次导入它:
_.reduce(/* ... */); //Works
_.reduce(/* ... */); // TypeError: Cannot call method 'reduce' of undefined
Run Code Online (Sandbox Code Playgroud) 至少,在Javascript中,在Chrome和Node.js上进行了测试:
new RegExp(/foo(optional)*boo/).exec('foooptionalboo')
Run Code Online (Sandbox Code Playgroud)
将与optional括号中的匹配:
[ 'foooptionalboo',
'optional',
index: 0,
input: 'foooptionalboo' ]
Run Code Online (Sandbox Code Playgroud)
但是如果你想在那之间有一些东西optional:
new RegExp(/foo.*(optional)*.*boo/).exec('foooptionalboo')
Run Code Online (Sandbox Code Playgroud)
然后optional找不到:
[ 'foooptionalboo',
'optional',
index: 0,
input: 'foooptionalboo' ]
Run Code Online (Sandbox Code Playgroud)
为什么是这样?
angular ×3
javascript ×3
node.js ×2
angularjs ×1
c++ ×1
express ×1
forms ×1
icu ×1
jasmine ×1
json ×1
pear ×1
pecl ×1
php ×1
protractor ×1
regex ×1
shell ×1
symfony ×1
typescript ×1
unit-testing ×1