I would like to know, how do I do in order to call a function in a promise in a fetch?
Please note this line: .then(json => this.processReq(json))
I have to use this.
because if don't it says that processReq
is undefined
. Shouldn't it be something like: .then(json => processReq(json))
because of ES6 ??
this is my code (I'm using Babel ES6 and React):
import React, { Component, PropTypes } from 'react'
import fetch from 'isomorphic-fetch'
export default class Batchprodpry extends Component {
constructor(props) {
super(props) {
.
.
.
processReq(json) {
Object.keys(json).forEach(item => {
if (item === 'error') {
Object.keys(json[item]).forEach(propry => {
alert('Conflicto! '+'\n'+
'Id: ' + JSON.stringify(json[item][propry]['propryid'])+'\n'+
'Id Operador: ' + JSON.stringify(json[item][propry]['fkempid'])+'\n'+
'Hora inicio: ' + JSON.stringify(json[item][propry]['propryhoraini'])+'\n'+
'Hora fin: ' + JSON.stringify(json[item][propry]['propryhorafin']))
})
}
})
}
.
.
postForm() {
let maq = this.state.obj['fkmaqid']
return fetch(`/scamp/index.php/batchprodpry/${maq}`, {
method: 'POST',
credentials: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(this.state.a)
})
.then(req => {
if (req.status >= 400) {
throw new Error("Bad response from server")
}
return req.json()
})
.then(json => this.processReq(json))
.catch(function(error) {
console.log('request failed', error)
})
}