Issue with Legacy Console's Inline Serverless Function Service

framework

#1

synchronous logging with console.log, works great and appears in the function logs, as expected

however, I cannot console.log things like resolved promises, async callbacks, etc because they don’t appear in the function logs… how/where can i see those console.logs?


#2

Can you share a code example for what you want to do, and what doesn’t work?


#3

pls see all of my console.log calls in the syntax below. In a nutshell, console logging does not work after promises are resolved, and I really need to see what the response looks like since it seems to be failing when POST requested to expo push api endpoint (https://exp.host/--/api/v2/push/send), but succeeding when POST requested to http://ptsv2.com/t/x620r-1523655299 ), and without resolved promise console.logs, I am troubleshooting blindly in the dark.

also, there is field included in every logged event named ‘response’, but its always an empty object, how can i make use of that?

furthermore, when you click on the question mark icon to view the docs for the legacy console functions (Graphcool projects), it points to a link that does not cover documentation for legacy console functions, but rather, functions docs for Graphcool service: https://www.graph.cool/docs/reference/functions/overview-aiw4aimie9/

links reference:
https://console.graph.cool/{project-name}/functions
https://console.graph.cool/{project-name}/functions/logs
https://console.graph.cool/{project-name}/functions/{id}/edit

require('isomorphic-fetch')

module.exports = function (event) {
  var uri = 'https://exp.host/--/api/v2/push/send'
  // var uri = 'http://ptsv2.com/t/x620r-1523655299/post'
  var res = event.data.Chat.node
  var msg = res.messages[0]
  var txt = msg.text
  var senderId = msg.writerx.id
  var senderName = `${msg.writerx.fbkFirstName || ''} ${msg.writerx.fbkLastName || ''}`
  var tokens = []
  
  function reshape(arr) {
  	arr.forEach(function(unit){
    	if (unit.userx.id !== senderId) tokens.push(unit.userx.pushToken)
    })	
  }
  
  reshape(res.distributorsx)
  reshape(res.shoppersx)
  
  function sendNotifications(data) {
    console.log('this prints in logs')
    return fetch(uri, {
      body: JSON.stringify(data),
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip, deflate'
      }
    }).catch(function(err) {console.log('this does not print in logs',err)})
  }
  
  var newTokens = []
  if (tokens.length <= 100) {
  	tokens.forEach(function(token){
		var notification = {
  			to: token,
    		body: `${senderName}: ${txt}`,
          	title: 'LC',
          	ttl: 3600000,
          	priority: 'high',
          	badge: 1
  		}
      	newTokens.push(notification)
    })
    console.log('this prints in logs')
    sendNotifications(newTokens)
    	.then(function(response) { return response.json() })
    	.then(function(data) { console.log('this does not print in logs',data) })
  } else {
  	// handle chunking
  }
  
}

#4

Instead of

.then(function(data) { console.log('this does not print in logs',data) })

try

.then(function(data) {
  // can't log multiple objects, only one
  console.log(data)
  return {
    data: {
      // whatever data you want to return - but it's important that you do.
    }
  }
})

And instead of

.catch(function(err) {console.log('this does not print in logs',err)})

try

.catch(function(err) {
   console.log(err)
   return {
      error: "Some error occurred"
   }
})

#5

unfortunately, copy & pasting in your syntax above, did not work either… the function logs are still producing this:
"logs": [], "returnValue": { "logs": [], "response": {} }


#6

Can you try to reduce the code to a minimal non-working example?


#7

this is the most minimal of non-working i can get it:

require('isomorphic-fetch')

module.exports = function (event) {
  
  var uri = 'https://exp.host/--/api/v2/push/send'
  
  function sendNotifications(data) {
    return fetch(uri, {
      body: JSON.stringify(data),
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
        'Accept-Encoding': 'gzip, deflate'
      }
    }).catch(function(err) {
    	console.log(err)
      	return {
        	error: err
        }	
    })
  }
  
  var newTokens = [
    {
      to: 'ExponentPushToken[...]',
      body: 'test notification',
      title: 'LipChat',
      ttl: 3600000,
      priority: 'high',
      badge: 1
    }
  ]
  
  sendNotifications(newTokens)
    .then(function(response) { return response.json() })
    .then(function(data) {
      console.log(data)
        return {
            data: data
          }
    })
  
}

#8

console.log after resolved promise does not seem to work with reduced/minimal version either


#9

just tested with the simplest of GET requests (below), and still not returning data or printing logs

require('isomorphic-fetch')

module.exports = function (event) {
  
  var uri = 'https://jsonplaceholder.typicode.com/users'
  
  function sendNotifications() {
    return fetch(uri, {
      method: 'GET'
    }).catch(function(err) {
    	console.log(err)
      return {
        error: err
      }
    })
  }
  
  sendNotifications()
    .then(function(response) { return response.json() })
    .then(function(data) {
      console.log(data)
      return {
        data: data
      }
    })
  
}

#10

This works:

require('isomorphic-fetch')

module.exports = function sum(event) {
  console.log('test')
  var uri = 'https://jsonplaceholder.typicode.com/users'

  return fetch(uri, {
      method: 'GET'
    })
    .then(function(response) { return response.json() })
    .then(function(data) {
      console.log(data)
      return {
        data: {
          sum: 3 
        }
      }
    })
}

It seems like the nested function/promise isn’t correctly resolved :slight_smile:


#11

thank you… this is definitely progress, but I have to iterate on that request, and unfortunately, it stops after the first ‘return’ … which is expected

trying to solve this with Promise.all([…http requests]) does not seem to work either

for clarity, this is my use case…
if i have 300 push notifications to send out, expo push notification api requires that i send out 100 notifications per request, for a total of 3 requests instead of spamming 300 requests… but having to ‘return fetch’ yields a response for the first request only, and the other 2 remaining requests are not handled

in your opinion, is this a limitation with isomorphic-fetch or with Auth0 Extend?

any guidance, workarounds, or suggestions?


#12

Are you able to solve this?
Can you share your code, I am also working on same. or pointers will be appreciated.


#13

Unfortunately Bamne123 I was never able to resolve… I ended up having to rely on a different Push Notification strategy that did not depend on the ‘isomorphic-fetch’ lib’s inability to return more than one promise. Also, sorry about the late reply… pls let me know if you were able to solve it, or if you also ended up switch to a different push notification strategy.