简体   繁体   中英

Node JS - How can I get the value outside the function?

I'm trying to get the value of the variable "Ajuste" outside the function, because I need Alexa to say it in the last return of the other function.

The value of the "Ajuste" variable should be used in the "speakOutput" variable that is outside that function, but finally it shows me that the value is undefined .

This is the fragment that I have problems with, the connection to the database is fine and the query is also executed without problems because the value that console.log(result.recordset [0].Consecutive);it is right.

var Inventario = {
  user: 'user',
  password: 'pass',
  server: 'server\\instance',
  database: 'db'
};

const InventarioIngresoIntentHandler = {
  canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' &&
      Alexa.getIntentName(handlerInput.requestEnvelope) === 'InventarioIngresoIntent';
  },
  handle(handlerInput) {
    var DocInventario = handlerInput.requestEnvelope.request.intent.slots.Documento.value
    var Ajuste;
    sql.connect(Inventario, function(err) {
      if (err) console.log(err);
      var request = new sql.Request();
      request.query('select * from Ajuste Where ConsecutivoExterno =' + DocInventario, function(err, result) {
        if (err) console.log(err)
        console.log(result.recordset[0].Consecutivo);
        console.log(result.recordset[0].ConsecutivoExterno);
        Ajuste = result.recordset[0].Consecutivo;
        sql.close();
      })
    });
    const speakOutput = Ajuste + ', Correcto.' + DocInventario
    return handlerInput.responseBuilder
      .speak(speakOutput)
      .withSimpleCard(SkillName, speakOutput)
      .reprompt(speakOutput)
      .getResponse();
  }
};

let the Ajuste property in a scope common to all the methods. I change var to const because var have an unexpected behavior sometimes because it isn't block scoped.

 const InventarioIngresoIntentHandler = { Ajuste: undefined, showAjuste() { this.Ajuste = "New Value"; console.log(this.Ajuste) }, canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' && Alexa.getIntentName(handlerInput.requestEnvelope) === 'InventarioIngresoIntent'; }, handle(handlerInput) { const DocInventario = handlerInput.requestEnvelope.request.intent.slots.Documento.value sql.connect(Inventario, function(err) { if (err) console.log(err); const request = new sql.Request(); request.query('select * from Ajuste Where ConsecutivoExterno =' + DocInventario, function(err, result) { if (err) console.log(err) this.Ajuste = result.recordset[0].Consecutivo; sql.close(); }) }); const speakOutput = this.Ajuste + ', Correcto.' + DocInventario return handlerInput.responseBuilder.speak(speakOutput).withSimpleCard(SkillName, speakOutput).reprompt(speakOutput).getResponse(); } }; InventarioIngresoIntentHandler.showAjuste()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM