Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/54.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 将对象传递给节点JS GET请求_Node.js_Angular_Http - Fatal编程技术网

Node.js 将对象传递给节点JS GET请求

Node.js 将对象传递给节点JS GET请求,node.js,angular,http,Node.js,Angular,Http,我正试图从angular应用程序向NodeJS服务器传递一个对象。我可以在客户端很好地读取对象,但在服务器端却看不到 这是我的客户端: var query = { date: '9-2-2019', size: 4 } this.http.get<any>(url, {params: {query: query} }).toPromise(); 是我的错误。请将{params:{query:query}}更改为{params:query},并将query.size更改为字

我正试图从angular应用程序向NodeJS服务器传递一个对象。我可以在客户端很好地读取对象,但在服务器端却看不到

这是我的客户端:

var query = {
  date: '9-2-2019',
  size: 4
}

this.http.get<any>(url, {params: {query: query} }).toPromise();

是我的错误。

请将
{params:{query:query}}
更改为
{params:query}
,并将
query.size
更改为字符串而不是数字

var query = {
  date: '9-2-2019',
  size: '4'
}


this.http.get<any>(url, {params: query}).toPromise().then(response => {
      console.log(response);
    })
    .catch(console.log);
然后在你的服务中使用它

import { UtilsService } from '/path/to/utils.service';

var query = {
  date: '9-2-2019',
  size: 4
}

 const queryParams: HttpParams = UtilsService.buildQueryParams(query);


this.http.get<any>(url, {params: queryParams }).toPromise().then(response => {
      console.log(response);
    })
    .catch(console.log);
从'/path/to/utils.service'导入{UtilsService};
变量查询={
日期:“2019年9月2日”,
尺码:4
}
const queryParams:HttpParams=UtilsService.buildQueryParams(查询);
this.http.get(url,{params:queryParams}).toPromise().then(response=>{
控制台日志(响应);
})
.catch(console.log);

我只是把它复制错了。现在它已关闭。使用then发送请求或订阅
this.http.get(url,{params:{query:query}}).subscribe(response=>{console.log(response);})
请提及完整的错误消息和nodejs web服务代码。没有重载与此调用匹配。类型“{query:{date:string;size:number;};}”不可分配给类型“HttpParams{[param:string]:string | string[];}”。请检查我的答案
import { HttpParams } from '@angular/common/http';
// ...
export class UtilsService {
    static buildQueryParams(source: Object): HttpParams {
        let target: HttpParams = new HttpParams();
        Object.keys(source).forEach((key: string) => {
      const value: string | number | boolean | Date = source[key];
            if ((typeof value !== 'undefined') && (value !== null)) {
                target = target.append(key, value.toString());
            }
        });
        return target;
    }
}
import { UtilsService } from '/path/to/utils.service';

var query = {
  date: '9-2-2019',
  size: 4
}

 const queryParams: HttpParams = UtilsService.buildQueryParams(query);


this.http.get<any>(url, {params: queryParams }).toPromise().then(response => {
      console.log(response);
    })
    .catch(console.log);