すぐ上のログステートメントを実行していますが、callback(null)
への呼び出しは機能していません。 try catchブロックでラップしてみましたが、何も見つかりませんでした。 firebaseはイベントループで何かを保っているようラムダnodeJS 4.3は成功コールバックを終了/実行していません
var Firebase = require('firebase');
var request = require('request');
//noinspection AnonymousFunctionJS
/**
*
* @param event - from Lambda
* @param context - from Lambda
* @param callback - from Lambda
*/
exports.handler = function (event, context, callback) {
var AUTOPILOT_API_KEY = getKey(event.stage, 'AUTOPILOT_API_KEY');
var AUTOPILOT_JOURNEY_ID = getKey(event.stage, 'AUTOPILOT_JOURNEY_ID');
activate();
function activate() {
console.log('event:', event);
if (validPayload(event, context)) {
addDefaultPresets(event.uid);
addToAutopilot(event.user, event.uid);
}
}
/**
* checks that the necessary payload has been received
* if YES: returns true and allows process to continue
* if NO: throws context.fail with useful error message(s)
* operating under custom error code naming convention of
* http code + 3 digit ULM error code
* @param event - from Lambda
* @param context - from Lambda
* @returns {boolean} - whether the payload contains the required data
*/
function validPayload (event, context) {
return true; // REDACTED FOR BREVITY
}
/**
* Adds the user to Autopilot as a contact and adds them
* to the journey with trigger id 0001
* @param {Object} user
* @param {string} uid generate by Firebase as unique identifier of registered user
*/
function addToAutopilot (user, uid) {
// REDACTED FOR BREVITY
request({
method: 'POST',
url: 'https://api2.autopilothq.com/v1/trigger/' + AUTOPILOT_JOURNEY_ID + '/contact',
headers: {
'autopilotapikey': AUTOPILOT_API_KEY,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
}, function (error, response, body) {
//noinspection MagicNumberJS
if (response.statusCode !== 200) {
errorResponse.status = response.statusCode;
errorResponse.error = {
errorMessage: error,
user: event.user,
response: response,
body: body
};
console.log('should throw ERROR callback');
context.fail(JSON.stringify(errorResponse));
} else {
console.log('should throw SUCCESS callback');
console.log(JSON.stringify({
status: response.statusCode,
message: "User successfully added to Autopilot account & journey"
}));
callback(null);
}
console.log('Finished addToAutopilot()');
});
}
/**
* Adds a collection of presets the the account of the new user
* @param uid {String} - Firebase UID
*/
function addDefaultPresets (uid) {
// REDACTED FOR BREVITY
var presets = ref.child('users').child(uid).child('presets');
console.log('Starting addDefaultPresets()');
activate();
function activate() {
console.info('activating...');
// for each field
fields.forEach(function (field) {
// iterate over each preset
presetData[field].forEach(function (label) {
// and add to firebase db via addDefaultPreset() if unique
presetIsUnique(field, label);
})
});
console.log('Finished addDefaultPresets()');
}
function presetIsUnique (field, label) {
presets.child(field).orderByChild('label')
.equalTo(label)
.once('value', function (snapshot) {
var val = snapshot.val();
if (!val) {
addDefaultPreset(field, label);
} else {
console.error('already exists', field, label);
}
});
}
function addDefaultPreset (field, label) {
//noinspection MagicNumberJS
presets.child(field).push().set({
creator: 'default',
dateAdded: Math.floor(Date.now()/1000),
label: label
}, setCallback);
}
function setCallback (err) {
if (err) {
console.error(err);
} else {
console.info('added preset');
}
}
}
function getKey (stage, keyId) {
var keys = {
AUTOPILOT_API_KEY: {
staging: 'dev123',
prod: 'prod123'
},
AUTOPILOT_JOURNEY_ID: {
staging: 'XXX',
product: 'XXXX'
},
FIREBASE_URL: {
staging: 'https://staging.firebaseio.com/',
prod: 'https://prod.firebaseio.com/'
}
};
if (stage === 'prod') {
return keys[keyId][stage];
} else {
return keys[keyId]['staging'];
}
}
};
「動作していない」とはどういう意味ですか?何が起こると予想されていますか、そして実際には何が起こっていますか?また、私はあなたが '' 'context.fail(JSON.stringify(errorResponse));' 'を使用していることに気付きましたが、この関数はnodejs4.3環境では利用できません。代わりにエラーを伴うcalbackを実行する必要があります'' 'コールバック(エラー)' '') –
@JonathanSeed私が '働いていない'と言ったとき、関数はタイムアウトするまで実行し続け、タイムアウトしたというエラーメッセージを返します。 それはタスクを完了しますが、それはコストがかかり、誤解を招くエラーを投げかけるよりも長く実行されます。 –
Gotcha。興味深い...私はアイデアを持っていますが、それは赤いニシンかもしれません。デフォルトでは、lambdaはプロセスがフリーズする前にイベントループが空になるまで待ちます。イベントループに何かがあって、機能が継続することはありますか?これをテストするには、 '' 'context.callbackWaitsForEmptyEventLoop = false;' 'を設定します。これにより、イベントループ内にイベントがあっても、コールバックが呼び出された直後にlambdaがプロセスをフリーズさせます。 –