如何将字符串参数从 angular UI 传递到 node.js 后端?
How to pass a string parameter from angular UI to node.js backend?
我正在尝试将字符串值从 angular UI 传递到 node.js 后端 api 然后使用传递的字符串值在 mongodb 中搜索如下。
我尝试在 enteredValue
中获取输入并将其作为 params:this.enteredValue
传递给 http.get
调用,并作为 req.params
传递给 node.js,正如你可以在下面看到如果我对 "orgChange" 的字符串值进行硬编码,它工作正常但不知何故传递参数不起作用并抛出错误?关于如何解决这个问题的任何指导?
html:
<textarea rows="6" [(ngModel)]="enteredValue"></textarea>
<hr>
<button (click)="get_change_lifecycle_data()">Search</button>
<p>{{newPost | json }}</p>
分量
import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Subject } from "rxjs";
import { map } from 'rxjs/operators';
@Component({
selector: 'app-change-input',
templateUrl: './change-input.component.html',
styleUrls: ['./change-input.component.css']
})
export class ChangeInputComponent {
constructor(private http: HttpClient) {}
enteredValue : any;
newPost : any;
get_change_lifecycle_data(){
this.http.get('http://localhost:3000/api/change_life_cycle2',{params:this.enteredValue}).subscribe(response => {
console.log(response);
this.newPost = response
});
}
}
node.js
硬编码 "orgChange" 的字符串值,它工作正常
app.get("/api/change_life_cycle", (req, res, next) => {
Change_life_cycle.find({ orgChange: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
API 与 req.params
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.params }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
错误:--
(node:75156) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{}" at path "orgChange" for model "change_life_cycle"
at new CastError (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/error/cast.js:29:11)
at SchemaString.cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schema/string.js:553:11)
at SchemaString.SchemaType.applySetters (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:948:12)
at SchemaString.SchemaType._castForQuery (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:1362:15)
at SchemaString.castForQuery (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schema/string.js:609:15)
at SchemaString.SchemaType.castForQueryWrapper (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:1331:15)
at cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/cast.js:252:34)
at model.Query.Query.cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:4576:12)
at model.Query.Query._castConditions (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:1783:10)
at model.Query.<anonymous> (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:1810:8)
at model.Query._wrappedThunk [as _find] (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
at process.nextTick (/Users/username/Downloads/mongodb-03-finished/node_modules/kareem/index.js:369:33)
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:75156) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:75156) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
编辑:获取参数的三种方式来表示:
- req.query --> 本例,查询参数
- req.body --> post 请求体负载
- req.params --> /:someParam in url params
所以我错过了 :) 它应该是 req.query.searchKey.
将查询更改为这样:您需要传递具有不同名称的不同参数:
this.http.get('http://localhost:3000/api/change_life_cycle2',
{
params:{
searchKey: this.enteredValue
}
}).subscribe(response => {
console.log(response);
this.newPost = response
});
在API端读取请求中的参数,如下所示:
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.query.searchKey }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
你可以尝试使用 HttpParams
:
get_change_lifecycle_data(){
const params = new HttpParams().set('params', this.enteredValue);
this.http.get('http://localhost:3000/api/change_life_cycle2',{params})
.subscribe(response => {
console.log(response);
this.newPost = response
});
}
并在 node
中使用 req.query
访问参数:
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.query.params }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
我正在尝试将字符串值从 angular UI 传递到 node.js 后端 api 然后使用传递的字符串值在 mongodb 中搜索如下。
我尝试在 enteredValue
中获取输入并将其作为 params:this.enteredValue
传递给 http.get
调用,并作为 req.params
传递给 node.js,正如你可以在下面看到如果我对 "orgChange" 的字符串值进行硬编码,它工作正常但不知何故传递参数不起作用并抛出错误?关于如何解决这个问题的任何指导?
html:
<textarea rows="6" [(ngModel)]="enteredValue"></textarea>
<hr>
<button (click)="get_change_lifecycle_data()">Search</button>
<p>{{newPost | json }}</p>
分量
import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from "@angular/common/http";
import { Subject } from "rxjs";
import { map } from 'rxjs/operators';
@Component({
selector: 'app-change-input',
templateUrl: './change-input.component.html',
styleUrls: ['./change-input.component.css']
})
export class ChangeInputComponent {
constructor(private http: HttpClient) {}
enteredValue : any;
newPost : any;
get_change_lifecycle_data(){
this.http.get('http://localhost:3000/api/change_life_cycle2',{params:this.enteredValue}).subscribe(response => {
console.log(response);
this.newPost = response
});
}
}
node.js
硬编码 "orgChange" 的字符串值,它工作正常
app.get("/api/change_life_cycle", (req, res, next) => {
Change_life_cycle.find({ orgChange: "51918661" }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
API 与 req.params
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.params }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
错误:--
(node:75156) UnhandledPromiseRejectionWarning: CastError: Cast to string failed for value "{}" at path "orgChange" for model "change_life_cycle"
at new CastError (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/error/cast.js:29:11)
at SchemaString.cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schema/string.js:553:11)
at SchemaString.SchemaType.applySetters (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:948:12)
at SchemaString.SchemaType._castForQuery (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:1362:15)
at SchemaString.castForQuery (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schema/string.js:609:15)
at SchemaString.SchemaType.castForQueryWrapper (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/schematype.js:1331:15)
at cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/cast.js:252:34)
at model.Query.Query.cast (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:4576:12)
at model.Query.Query._castConditions (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:1783:10)
at model.Query.<anonymous> (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/query.js:1810:8)
at model.Query._wrappedThunk [as _find] (/Users/username/Downloads/mongodb-03-finished/node_modules/mongoose/lib/helpers/query/wrapThunk.js:16:8)
at process.nextTick (/Users/username/Downloads/mongodb-03-finished/node_modules/kareem/index.js:369:33)
at process._tickCallback (internal/process/next_tick.js:61:11)
(node:75156) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:75156) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
编辑:获取参数的三种方式来表示:
- req.query --> 本例,查询参数
- req.body --> post 请求体负载
- req.params --> /:someParam in url params
所以我错过了 :) 它应该是 req.query.searchKey.
将查询更改为这样:您需要传递具有不同名称的不同参数:
this.http.get('http://localhost:3000/api/change_life_cycle2',
{
params:{
searchKey: this.enteredValue
}
}).subscribe(response => {
console.log(response);
this.newPost = response
});
在API端读取请求中的参数,如下所示:
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.query.searchKey }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});
你可以尝试使用 HttpParams
:
get_change_lifecycle_data(){
const params = new HttpParams().set('params', this.enteredValue);
this.http.get('http://localhost:3000/api/change_life_cycle2',{params})
.subscribe(response => {
console.log(response);
this.newPost = response
});
}
并在 node
中使用 req.query
访问参数:
app.get("/api/change_life_cycle2", (req, res, next) => {
console.log(req.body)
Change_life_cycle.find({ orgChange: req.query.params }).then(documents => {
res.status(200).json({
message: "Posts fetched successfully!",
posts: documents
});
});
});