gpt4 book ai didi

firebase - Lambda nodeJS 4.3 未完成/执行成功回调

转载 作者:行者123 更新时间:2023-12-02 18:57:53 26 4
gpt4 key购买 nike

尽管运行了上面的日志语句,但我对 callback(null) 的调用不起作用。甚至尝试将其包装在 try catch block 中,但一无所获。

作为引用,以下是完整功能:

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'];
}
}
};

最佳答案

Firebase 似乎在事件循环中保留了某些内容。默认情况下,lambda 会等到事件循环为空才终止。您可以设置 context.callbackWaitsForEmptyEventLoop = false 来告诉 lambda 在调用回调后立即终止该函数,即使事件循环中仍然有项目。

关于firebase - Lambda nodeJS 4.3 未完成/执行成功回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38987129/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com