简体   繁体   English

发送推送通知时解析服务器云代码错误

[英]Parse Server Cloud Code error when sending push notification

Using Parse Cloud Code on back4app, I am trying to send a push notification after an object is saved:在 back4app 上使用 Parse Cloud Code,我试图在保存 object 后发送推送通知:

Parse.Cloud.afterSave('PFActivity', async () => {
  const query = new Parse.Query(Parse.Installation);
  query.exists('deviceToken');
  await Parse.Push.send({
  where: query,
  data: { alert: 'notification' },
  useMasterKey: true
}).then(function() {
    // Push was successful
      console.log('Sent push.');
  }, function(error) {
      console.log('error push.');
      throw "Push Error " + error.code + " : " + error.message;
  });
});

I am getting the following error:我收到以下错误:

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:526:11) at ServerResponse.header (/usr/src/app/node_modules/express/lib/response.js:771:10) at ServerResponse.send (/usr/src/app/node_modules/express/lib/response.js:170:12) at ServerResponse.json (/usr/src/app/node_modules/express/lib/response.js:267:15) at /usr/src/app/src/back/app.js:219:9 at Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:71:5) at trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:315:13) at /usr/src/app/node_modules/express/lib/router/index.js:284:7 at Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12) at next (/usr/src/app/node_modules/express/lib/router/index.js:275:10)错误 [ERR_HTTP_HEADERS_SENT]: 在 ServerResponse.header (/usr/src/app/node_modules/express/lib/response.js: 771:10) 在 ServerResponse.send (/usr/src/app/node_modules/express/lib/response.js:170:12) 在 ServerResponse.json (/usr/src/app/node_modules/express/lib/response. js:267:15) 在 /usr/src/app/src/back/app.js:219:9 在 Layer.handle_error (/usr/src/app/node_modules/express/lib/router/layer.js:71 :5) 在 trim_prefix (/usr/src/app/node_modules/express/lib/router/index.js:315:13) 在 /usr/src/app/node_modules/express/lib/router/index.js:284 :7 在 Function.process_params (/usr/src/app/node_modules/express/lib/router/index.js:335:12) 在下一个 (/usr/src/app/node_modules/express/lib/router/index. js:275:10)

I would appreciate any suggestions for troubleshooting this error.对于解决此错误的任何建议,我将不胜感激。 Thanks.谢谢。

It appears I had the formatting incorrect.看来我的格式不正确。 This is working-这是有效的-

Parse.Cloud.afterSave('PFActivity', async () => {
  const query = new Parse.Query(Parse.Installation);
  query.exists('deviceToken');
  await Parse.Push.send({
  where: query,
  data: { alert: 'notification' }
  },
      {useMasterKey: true}).then(function() {
    // Push was successful
      console.log('Sent push.');
  }, function(error) {
     // console.log('error push.');
      console.log("Push Error " + error.code + " : " + error.message);
      throw "Push Error " + error.code + " : " + error.message;
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM