【问题标题】:Add object to request body from angular to nodejs将对象添加到从角度到 nodejs 的请求正文
【发布时间】:2021-06-22 01:00:00
【问题描述】:

我有一个这种格式的 yaml 文件:

Layouts:
        -
          Name: Default Layout
          LayoutId : 1
          ConfiguredSegments:
            LiveA :
                Height : 100
                Id : LiveA
            Ref1A :
                Height : 100
                Id : Ref1A

我的 nodejs 服务器端有一个类似的函数,用于替换我的 yaml 文件中的“ConfiguredSegments”字段:

//function to update specific layout based on LayoutId
export const updateSpecificLayout = (req, res)=>{
    const { id } = req.params;
    const { ConfiguredSegments } = req.body;

    const getLayoutList = JSON.parse(JSON.stringify(layoutData)); 

    const layoutToBeUpdated = getLayoutList.Layouts.find((layout) => layout.LayoutId == id );

    findAndReplace(getLayoutList.Layouts,layoutToBeUpdated.ConfiguredSegments,ConfiguredSegments)

      let yaml = YAML.dump(getLayoutList);
     
       fs.writeFileSync("layouts.yaml", yaml, function (err,file){
          if(err) throw err;        
          console.log(`Layout with the id:${id} has been updated`);
  })      

    res.send(`Layout with the id:${id} has been updated`);
    console.log(`Layout with the id:${id} has been updated`);
}

从角度来看,我正在尝试更新配置的段:

conseg:any;
confseglist:any;

conseg = {
"ConfiguredSegments": {
"SomeSegment": {
"Height": 9999,
"Id": "Hello"
}
}

//appSettingsService is my service instance
 this.appSettingsService.updateSpecificLayout ("1").subscribe(data => {   

  this.confseglist= Object.values(data);
  //Here i want to add body as 'conseg ' to my patch request
  
 })

我的http服务类:

我知道我必须在体内传递我的“conseg”。

//Update specific layout based on layout ID
updateSpecificLayout(id: string) {
    const urlLayout = `${this._url}/${id}`;
    return this.http.patch(urlLayout, conseg ).pipe(catchError(this.errorHandler))
}

但我不知道如何将正文添加到我的补丁请求中

【问题讨论】:

  • this.http.patch(urlLayout, conseg) - 这里 conseg 是请求正文。它有什么问题?您想在正文中添加其他内容吗?
  • 我收到此错误“错误 TS2304:找不到名称 'conseg'。35 return this.http.patch(urlLayout, conseg).pipe(catchError(this.errorHandler))”
  • 定义一个额外的参数,如updateSpecificLayout(id: string, conseg: any),并从组件updateSpecificLayout("1", conseg)传入。

标签: node.js angularjs angular http


【解决方案1】:

这一行:

this.appSettingsService.updateSpecificLayout("1").subscribe(data => {

更新到:

this.appSettingsService.updateSpecificLayout("1", conseg).subscribe(data => {

并将您的服务更新为:

updateSpecificLayout(id: string, conseg: any) {

【讨论】:

  • 这适用于我以及 @Micheal D 的更改。
猜你喜欢
  • 1970-01-01
  • 2017-01-15
  • 2019-01-29
  • 1970-01-01
  • 2019-06-26
  • 1970-01-01
  • 1970-01-01
  • 2015-05-02
  • 1970-01-01
相关资源
最近更新 更多