我一直在尝试将我在'request body'中收到的一些数据发送到我正在使用节点js中的fetch调用的外部API,但是出了点问题我不知道数据是否是我的是否正在通过该外部API正确接收发送。这就是我的控制器的外观:
'use strict';
var mongoose = require('mongoose'),
fetch = require('node-fetch'),
Fault = mongoose.model('Fault');
exports.register_a_fault = function(req, res) {
var new_fault = new Fault(req.body);
new_fault.save(function(err, fault) {
if (err)
res.send(err);
console.log(fault.machineId);
res.json({dataId:req.body.dataId});
fetch('http://localhost/fault', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: '{"machineId":req.body.machineId,"start":req.body.startDateTime,"clientEmail":"a@xyz.in"}'
}).then(function(res){
console.log(res);
}).catch(err => {});
});
};
register_a_fault的架构是
{
"machineId":"test",
"startDateTime":"2017-11-23 17:36:00",
"endDateTime":"2017-11-23 17:46:00"
}
以及外部故障API的架构
{
"machineId":"test",
"start":"2017-11-23 17:36:00",
"clientEmail":"a@xyz.in"
}