0

Following the documentation, I am using this in my JS:

var store = StoreClient('my secret');
store.set('active', true);
var status = store.get('active');

The variable status never has a value. I'm clearly not using the library correctly.

For context, this is inside a switch statement that does something like this for many of the cases, where some of them need to set or get a value from the StoreClient.

The documentation uses this example:

var store = StoreClient('your secret here');
store
  .set('hello', 'world')
  .then(function() {
    return store.get('hello');
  })
  .then(function(value) {
    // value === 'world'
    return store.delete('hello');
  })
  .then(function() {
    callback();
  })
  .catch(callback);

Because I'm on the amateur side, I'm not super familiar with promises. In that example, it's unclear to me which parts of the are required in order to [a] set, and eventually, [b] get a value. I suggest including an example that doesn't have set/get/delete combined into one.

I tried this:

var store = StoreClient('my secret');
store
  .set('active', true)
  .then(function() {
    return store.get('active');
  })
  .then(function() {
    callback();
  })
  .catch(callback);

... but then I get an error that there is no output variable, even though I haven't touched the output variable at the bottom of the script.

1 Answers1

0

David from Zapier's Platform team here.

Sorry about the confusion in the docs. I'll give you a quick answer on how to fix your code and a long one as to why. In the meantime, I'll make a note to update the docs with more sensical examples.

Short

Two big things:

A. Promises pick whatever was returned in the last function. If you don't bring them along, they're lost. Your code should read:

.then(function(storedVal) { // <- that variable is missing in your code
  console.log('stored val is', storedVal);
})

B. You need to provide a value to the second argument of callback. There's a better example here.

.then(function(storedVal) {
  callback(null, {active: storedVal});
})

Long

Here's some of the nitty gritty on how to make all Zapier code work great.

Callback

Your code runs inside AWS Lambda, which always needs to know when you're finished. It executes all of your code in a special function with a certain set of arguments. The pertinent one here is callback, a function that you can call when you're ready to exit (or have an error). You can read more about that setup here.

Like most node callbacks, the callback has the function signature callback (error, result). To throw an error, you pass something in the first spot:

callback({msg: 'thing went wrong'});

To pass a result, use the second (and nothing in the first)

callback(null, {myData: 4});

So, not passing anything there is why the zap result isn't seeing any data.

Promises

In general, callbacks suck and are confusing to work with, so we designed StoreClient to return promises. There's a lot of materials about promises online so I won't go into the details here. The important thing is that whatever gets returned from a promise's function is the argument in the next one. For example:

Promise.resolve(1)
  .then(function(val) {
    // val === 1
    return Promise.resolve(val + 1)
  })
  .then(function(val) {
    // val === 2
  })

There's a more practical example in these docs:

var store = StoreClient('your secret here');
var outCount;
store
  .get('some counter')
  .then(function(count) {
    count = (count || 0) + 1;
    outCount = count;
    return store.set('some counter', count);
  })
  .then(function() {
    callback(null, {'the count': outCount});
  })
  .catch(callback);

Hopefully that clears things up a bit!

Also, if you want to give Python a try, you can do the same code, but much simpler (example here).

Either way, let us know if there's anything else we can do to help!

xavdid
  • 5,092
  • 3
  • 20
  • 32
  • To avoid possibly having called `callback` twice when it throws, it's [better style](https://stackoverflow.com/q/24662289/1048572) to use `.then(outCount => callback(null, {'the count': outCount}), err => callback(err))`. – Bergi May 25 '17 at 17:00