【问题标题】:sending data from angular to node api从角度发送数据到节点api
【发布时间】:2019-09-07 16:49:03
【问题描述】:

我是初学者,如果我的问题不相关或不易于解决,我深表歉意,但我无法解决我的问题,4 天之内。

我正在开发一个 api,它将接收角度形式的数据,然后将其与 sequelize 一起存储在 mariadb 表中。

当我通过 url 提交表单时,通过 api 将其发布到表中,我可以看到 req.body 已定义,并到达 api,但我的角度有错误抛出的 http 帖子(错误 500)。 使用邮递员,我发现我的网址无法访问,我无法弄清楚我的错误是来自帖子角度还是来自我的模型的后端,即使错误 500 是针对后端的。

我尝试重新定义我的 sequelize 模型,解析和字符串化我发送的对象,尝试其他 url...但没有任何效果。

所以这是我在提交表单时调用的函数,函数格式requestschedulest返回我要发布的对象。

onSubmitScheduled(){
    let OndemandScheduledRequest = this.formatRequestScheduled(this.selectedHotel, this.selectedCheckInDate, this.selectedCheckOutDate, this.selectedNumber, this.selectedCurrency, this.selectedReportName, this.selectedUser, this.selectedEmail, this.selectedFormat);

    console.log(OndemandScheduledRequest);
    this.saveScheduledRequest(OndemandScheduledRequest);
    return this.openDialog();
  }


    saveScheduledRequest(onDemandScheduledRequest: OnDemandScheduledRequest){
       this.http.post('/api/ScheduledReport', JSON.stringify(onDemandScheduledRequest), {headers: {'Content-Type': 'application/json'}})
         .subscribe(res=>{ console.log(res
         )
           console.log('ok')},
         (err) =>
           console.log('an error'))
           /*console.log(err))*/
    }

这是我的 api:

var request= require('../controller/ondemandscheduledrequests');
var router = express.Router();


router.get('/getreport',request.create);
router.post('/ScheduledReport',request.create);

我也可以提供我得到的错误,但我不知道我是否可以在问题中加入图片。

我只想将我的表单数据存储到我的表中,但我得到了一个错误 500。

this is the error i get

【问题讨论】:

标签: node.js angular sequelize.js


【解决方案1】:

使用如下管道并尝试一次

  this.http.post('/ScheduledReport', JSON.stringify(onDemandScheduledRequest).pipe().subscribe(val => {
            console.log(val, "service");
          , {headers: {'Content-Type': 'application/json'}})}

【讨论】:

  • 这是因为我使用路由器作为子路径,所以我的服务器配置文件中有一个 app.use('/api',api)
  • 您是否在运行客户端代码(ui)的同时运行服务器代码(后端)
猜你喜欢
  • 1970-01-01
  • 2018-03-24
  • 2016-05-19
  • 2021-08-22
  • 1970-01-01
  • 2018-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多