我最近是一名PHP程序员,为了使一个项目完成,我已经惊讶地使用nodejs了,不幸的是,还可以使用dynamo db,所以在这里我遇到了一个问题,我知道这可能是基本的如此愚蠢
正在执行的代码在这里
if (typeof(req.body.question) == 'object') {
quest = req.body.question;
}
if (typeof(req.body.question) == 'string') {
quest = req.body.question;
}else{
questionsq = {
'ConsistentRead': true,
TableName : 'testquestions',
KeyConditionExpression: "testid = :testid",
ExpressionAttributeValues: { ":testid":{'S':'f3b21bf0-d6b9-11e8-bdf1-f7fcc44e7f9c'} }
};
vulog.info('Question is not settled so querying',questionsq);
dynamodb.query(questionsq, function(err,quest){
vulog.info('The data:',JSON.stringify(quest));
if(err || !quest ){
vulog.error('Query went wrong',err);
}else{
vulog.info('Query went fine',JSON.stringify(quest));
//return quest;
}
});
}
在此之后,我希望这段代码能够正常工作。
var originalQuestions = [];
vulog.debug('--------------------');
if (!quest || quest.length == 0) {
vulog.warn('Questions not entered');
errstr += '\nAt least one interview question must be entered';
} else {
//Goes on
目前,我正在终端中接收该消息,并且卡在了那里,没有进一步执行。
Query went fine {"Items":[{//Datas}],"Count":2,"ScannedCount":2}
我要的是我要执行代码的第一部分并给出quest的值,然后继续执行代码。我该怎么办现在它正在执行并卡在那 vulog.info('查询很好',JSON.stringify(quest));没有进一步的处决:)
答案 0 :(得分:0)
编辑:
异步/等待方法:
async function() {
........
var quest = await queryQuestion();
var originalQuestions = [];
vulog.debug('--------------------');
if (!quest || quest.length == 0) {
vulog.warn('Questions not entered');
errstr += '\nAt least one interview question must be entered';
} else {
//Goes on
}
}
原始答案:
您可以将第一个逻辑包装在函数中,然后返回一个Promise,然后将resolve
转换为quest
或reject
和error
function queryQuestion() {
return new Promise(function(resolve, reject) {
if (typeof(req.body.question) == 'object' || typeof(req.body.question) == 'string') {
quest = req.body.question;
resolve(quest);
} else {
questionsq = {
'ConsistentRead': true,
TableName: 'testquestions',
KeyConditionExpression: "testid = :testid",
ExpressionAttributeValues: {
":testid": {
'S': 'f3b21bf0-d6b9-11e8-bdf1-f7fcc44e7f9c'
}
}
};
vulog.info('Question is not settled so querying', questionsq);
dynamodb.query(questionsq, function(err, quest) {
vulog.info('The data:', JSON.stringify(quest));
if (err || !quest) {
vulog.error('Query went wrong', err);
reject(error);
} else {
vulog.info('Query went fine', JSON.stringify(quest));
//return quest;
resolve(quest);
}
});
}
})
}
然后调用此函数
queryQuestion().then(function(quest){
var originalQuestions = [];
vulog.debug('--------------------');
if (!quest || quest.length == 0) {
vulog.warn('Questions not entered');
errstr += '\nAt least one interview question must be entered';
} else {
//Goes on
}
}, function(error){
// Handle errors
})
希望这对您有所帮助。