У меня есть два метода, которые имеют общую логику ответа, и я попытался извлечь эту логику ответа другому методу и связать ее со всеми обещаниями, но выдал ошибки:
Исходные методы:
method1: function (req, res) {
db.getData(req)
.then(data => {
res.status(200).send({ status: 200, data: data })
})
.catch(error => {
res.status(500).send({ status: 500, statusText: error.message })
})
},
method2: function (req, res) {
db.getData2(req)
.then(data => {
res.status(200).send({ status: 200, data: data })
})
.catch(error => {
res.status(500).send({ status: 500, statusText: error.message })
})
},
Что я пытался сделать? (Извлечь обещание ответа для другого распространенного метода)
responseMethod: function (promise) {
promise
.then(data => {
res.status(200).send({ status: 200, data: data })
})
.catch(error => {
res.status(500).send({ status: 500, statusText: error.message })
})
},
method1: function (req, res) {
responseMethod(db.getData(req))
},
method2: function (req, res) {
responseMethod(db.getData2(req))
},
Ошибка:
Reference Error: responseMethod is not defined