[ERR_HTTP_HEADERS_SENT] on Cloud function

  1. Hello, We are getting this error on our cloud function 2022-02-17T19:49:15.886Z - Error generating response.
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at new NodeError (node:internal/errors:371:5) at ServerResponse.setHeader (node:_http_outgoing:576:11) at ServerResponse.header (/moralis-server/node_modules/express/lib/response.js:771:10) at ServerResponse.send (/moralis-server/node_modules/express/lib/response.js:170:12) at ServerResponse.json (/moralis-server/node_modules/express/lib/response.js:267:15) at /moralis-server/lib/PromiseRouter.js:214:13 at runMicrotasks (<anonymous>) at processTicksAndRejections (node:internal/process/task_queues:96:5) { code: 'ERR_HTTP_HEADERS_SENT' }

Here is our cloud functions.

 const addWei = (weiArray = []) => {
    // eg ['4553535353535', '20000000000', '50000']
    const add = (a, b) => {
      const amount = toBN(a)
      const amountToAdd = b ? toBN(b) : false
      if (amountToAdd) {
        return amount.add(amountToAdd).toString()
        // return wei string value
      // return wei string value
      return amount.toString()
    const sumInWei = weiArray.reduce((acc, v, i) => {
      if (!acc) return v
      return add(acc, v)
    }, '')
    return sumInWei
const unconfirmedAmaUserAddressPresent = async (_object, _key) => {
    let User = Moralis.Object.extend('UnconfirmedAmaUser')
    let query = new Moralis.Query(User)
    query.equalTo('address', _object.get(_key).toString())
    let user = await query.first()
    if (user == undefined) {
      return false
    } //User is not present
    return user //User is present

  const updateUnconfirmedUserQuestionReceived = async (user, value) => {

    let Value = addWei([unconfirmedUser.get('valueReceivedOnQuestions'), value])
    user.set('valueReceivedOnQuestions', Value)
      (session) => {
        logger.info('updateUnconfirmedUserQuestionReceived: User updated')
      (error) => {
          'updateUnconfirmedUserQuestionReceived: Failed to create new object, with error code: ' +

  Moralis.Cloud.afterSave('QuestionCreated', async function (request) {
      let confirmed = request.object.get('confirmed')
      if (confirmed) {
          let unconfirmedUser = await unconfirmedAmaUserAddressPresent(
          if (unconfirmedUser) {
              unconfirmedUser, request.object.get("value")

Do you know in particular what cloud function generates that error?

This function updateUnconfirmedUserQuestionReceived

We have been trying to resolve it for quite soemtime.

Were you able to identify what line generates that error?

NO…Havent been able to.

You can add more logging with logger.info, and this way you can get closer to where it comes from

Now Moralis server has stopped taking new events.!!

I’m going to need the server url, what events stoped working?


createQuestion event

now it is back and syncing events

  1. 2022-02-17T21:15:31.859Z - Warning: Duplicate cloud functions exist for unwatchAvaxAddress. Only the last one will be used and the others will be ignored.

Unwanted warnings on our logs are showing up. Should we ignore them?

yes, you can ignore those ones

Our server has again stopped syncing events. Can you please check?

1 Like

it seems to work, it also seems that historical sync is disabled now for that event

It is behaving randomly,
Events are not syncing sequentially but randomly.

Look at the block timestamp.

Some events arent showing up rightaway and then suddenly all events starts showing up together with jumbled up timestamps?

Is this the problem with free servers?

it shouldn’t be a problem with a free server, you have only few events

it looks like the events arrive in the proper order, if you sort them you will get proper order, but they do have some delay