Using meteor-methods in interval, serverside (done)

Hi,

I’m working on a thing that will wipe the messages in a chat every 6 hours.


Meteor.setInterval(function(){  
  const text = "Messages deleted."
  const character = "[Bot]"; 
  const room = "OOC";
  const fs = null;
  const whisperTo = 0;
  const fontColor = "#4287f5";
  var d = new Date();
  var curr_hours = d.getHours();
  var curr_minutes = d.getMinutes();
  var curr_seconds = d.getSeconds();
  var currentHours = ("0" + curr_hours).slice(-2);
  var currentMinutes = ("0" + curr_minutes).slice(-2);
  var currentSeconds = ("0" + curr_seconds).slice(-2);
  var todayDate = Date.now();
  var displayDate = (currentHours + ":" + currentMinutes);
  var dice = undefined;
  // Method for deleting all chat messages
  Meteor.call('delete.all.msg.ADMINTESTONLY');
  // Method for message that informs user about the wipe that just happened
  Meteor.call('chat.start', text, character, room, fs, whisperTo, fontColor, displayDate, todayDate, dice);
}, 5000);


I put that on the serverside where I try to call two methods, one for deleteing the messages, the other to inform the users. Now when I check this on the client it works like intended. The console gives me this error though:

I20191116-00:58:28.559(1)? Exception in setInterval callback: { Error: [not-authorized]
I20191116-00:58:28.559(1)?     at MethodInvocation.delete.all.msg.ADMINTESTONLY (imports/api/chat/methods.js:393:35)
I20191116-00:58:28.560(1)?     at maybeAuditArgumentChecks (packages/ddp-server/livedata_server.js:1771:12)
I20191116-00:58:28.560(1)?     at DDP._CurrentMethodInvocation.withValue (packages/ddp-server/livedata_server.js:1689:15)
I20191116-00:58:28.560(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1234:12)
I20191116-00:58:28.561(1)?     at resolve (packages/ddp-server/livedata_server.js:1687:36)
I20191116-00:58:28.561(1)?     at new Promise (<anonymous>)
I20191116-00:58:28.561(1)?     at Server.applyAsync (packages/ddp-server/livedata_server.js:1686:12)
I20191116-00:58:28.562(1)?     at Server.apply (packages/ddp-server/livedata_server.js:1625:26)
I20191116-00:58:28.562(1)?     at Server.call (packages/ddp-server/livedata_server.js:1607:17)
I20191116-00:58:28.562(1)?     at imports/api/chat/chat.js:46:10
I20191116-00:58:28.563(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1234:12)
I20191116-00:58:28.563(1)?     at packages\meteor.js:550:25
I20191116-00:58:28.563(1)?     at runWithEnvironment (packages\meteor.js:1286:24)
I20191116-00:58:28.563(1)?  => awaited here:
I20191116-00:58:28.563(1)?     at Promise.await (C:\Users\NERV\AppData\Local\.meteor\packages\promise\0.11.2\npm\node_modules\meteor-promise\promise_server.js:60:12)
I20191116-00:58:28.564(1)?     at Server.apply (packages/ddp-server/livedata_server.js:1638:14)
I20191116-00:58:28.564(1)?     at Server.call (packages/ddp-server/livedata_server.js:1607:17)
I20191116-00:58:28.564(1)?     at imports/api/chat/chat.js:46:10
I20191116-00:58:28.565(1)?     at Meteor.EnvironmentVariable.EVp.withValue (packages\meteor.js:1234:12)
I20191116-00:58:28.568(1)?     at packages\meteor.js:550:25
I20191116-00:58:28.569(1)?     at runWithEnvironment (packages\meteor.js:1286:24)
I20191116-00:58:28.569(1)?   isClientSafe: true,
I20191116-00:58:28.569(1)?   error: 'not-authorized',
I20191116-00:58:28.569(1)?   reason: undefined,
I20191116-00:58:28.570(1)?   details: undefined,
I20191116-00:58:28.570(1)?   message: '[not-authorized]',
I20191116-00:58:28.570(1)?   errorType: 'Meteor.Error' }
I20191116-00:58:28.572(1)? Exception in callback of async function: TypeError: callback is not a function
I20191116-00:58:28.573(1)?     at packages/mongo/collection.js:729:7
I20191116-00:58:28.574(1)?     at runWithEnvironment (packages\meteor.js:1286:24)
I20191116-00:58:28.574(1)?     at packages\meteor.js:1299:14
I20191116-00:58:28.575(1)?     at packages/mongo/mongo_driver.js:331:7
I20191116-00:58:28.575(1)?     at runWithEnvironment (packages\meteor.js:1286:24)

Anyone can tell me what this means? I would appreciate any help!

How does delete.all.msg.ADMINTESTONLY perform authorization? If it relies on this.userId, and you invoke from the server, that could be your problem, because this.userId won’t have a value.

1 Like

Ah right, thank you. Had to change both methods a bit. The console-error grew smaller, however, this remains:

I20191116-06:06:57.844(1)? Exception in callback of async function: TypeError: callback is not a function
I20191116-06:06:57.844(1)?     at packages/mongo/collection.js:729:7
I20191116-06:06:57.845(1)?     at runWithEnvironment (packages\meteor.js:1286:24)
I20191116-06:06:57.845(1)?     at packages\meteor.js:1299:14
I20191116-06:06:57.846(1)?     at packages/mongo/mongo_driver.js:331:7
I20191116-06:06:57.846(1)?     at runWithEnvironment (packages\meteor.js:1286:24)

I assume it has something to do with the interval-function?

Server-only code should not be accessible from the client at all. See

That’s a pretty cryptic trace. Seems like it involves a DB query/op. Might be necessary to add some console.logs to determine the offending line of code, or experiment in meteor shell, or use https://docs.meteor.com/commandline.html#meteordebug.

Alright, thank you. For now I decided to activate this function in a manual way, not set by an interval.