最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

在另一个API中调用API会返回错误[ERR

运维笔记admin6浏览0评论

在另一个API中调用API会返回错误[ERR

在另一个API中调用API会返回错误[ERR

我正在设置一些API,我需要从一个API调用另一个已配置的API。这是我必须打电话给的那个:

exports.createNews = function (req, res, next) {
var notification = new Notification(req.body);

notification.save(function (err, notification) {
    if (err)
        return res.send(new Error(500, "There was a problem creating the notifications", err));

    res.send({
        success: true,
        data: {
            notification: notification
        }
    });
});

};

这是调用另一个的API:

feedback.save(function (err, feedback) {
                if (err)
                    return res.status(400).send(new Error(400, "Validation failed. Invalid attributes", err));

                var notification = new Notification()

                createNews(req,res,function () {
                    return res.status(200).send({
                        success: true,
                        data: {
                            feedback: feedback
                        }
                    });
                })
            })

我收到此错误:错误[ERR_HTTP_HEADERS_SENT]:将标头发送到客户端后无法设置标头

问题是我还必须单独打第一个电话。

感谢您的帮助。

回答如下:

您两次在res.send中调用一次createNews,在feedback.save中调用一次。错误Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client通常表示您已经发送了一次响应,并且出于明显的原因,您无法再次发送响应。因此,您需要做的是仅从createNewsfeedback.save中的一个功能发送响应。希望能回答您的问题。

发布评论

评论列表(0)

  1. 暂无评论