Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 将查询结果保存到变量Alexa Skills Json中_Mysql_Json_Variables_Alexa Skills Kit_Alexa Skill - Fatal编程技术网

Mysql 将查询结果保存到变量Alexa Skills Json中

Mysql 将查询结果保存到变量Alexa Skills Json中,mysql,json,variables,alexa-skills-kit,alexa-skill,Mysql,Json,Variables,Alexa Skills Kit,Alexa Skill,我需要一个用于alexa应用程序的DB,所以我设置了它,它插入得很好,但当我试图选择并保存到变量时,保存到变量的值是[Object Object],而不是想要的值,我知道这可能是异步问题或解析问题,但我无法修复代码,一些帮助会很酷 canHandle(handlerInput) { return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest' &&

我需要一个用于alexa应用程序的DB,所以我设置了它,它插入得很好,但当我试图选择并保存到变量时,保存到变量的值是[Object Object],而不是想要的值,我知道这可能是异步问题或解析问题,但我无法修复代码,一些帮助会很酷

    canHandle(handlerInput) {
        return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
            && Alexa.getIntentName(handlerInput.requestEnvelope) === 'buscaIntent';
    },
    handle(handlerInput) {

            const mysql = require('mysql');
            const connection = mysql.createConnection
            ({
            host: 'remotemysql.com',
            user: 'RBb34534sd',
            password: 'xxxxxxxxx',
            database: 'RBsdfewrg'
            });


            var stat = connection.query('SELECT `spe` FROM `prueba` WHERE `nombre` LIKE "raichu" limit 1', function (err, result, fields) {
                if (err) throw err;
                console.log(result);
                return result[0];
              });
            connection.end();

            return handlerInput.responseBuilder

            .speak("Busc " + stat)
            .reprompt("reprompt buscar")
            .getResponse();


    }
}; ```

我认为查询返回的是一个对象,您无法将该对象保留在语音中。检查对象内部的内容,如果您希望在该对象内部有一个字段,则通过
stat.YourField

访问。问题是您没有等待数据库查询完成,然后再将响应发送到Alexa服务。node.js中的请求是非阻塞的,这意味着您需要使用回调嵌套请求,或者利用Promissions/async Wait模式,以便在函数完全执行之前处理SQL查询

您可以阅读更多关于将SQL连接的内置库转换为支持承诺的内容,或者使用已经有包装器的库

在任何一种情况下,最终结果都会被重构为如下内容:

canHandle(handlerInput) {
    return Alexa.getRequestType(handlerInput.requestEnvelope) === 'IntentRequest'
        && Alexa.getIntentName(handlerInput.requestEnvelope) === 'buscaIntent';
},
async handle(handlerInput) {
        const mysql = require('mysql2/promise');
        const connection = await mysql.createConnection
        ({
        host: 'remotemysql.com',
        user: 'RBb34534sd',
        password: 'xxxxxxxxx',
        database: 'RBsdfewrg'
        });

        var stat = await connection.execute('SELECT `spe` FROM `prueba` WHERE `nombre` LIKE "raichu" limit 1', function (err, result, fields) {
            if (err) throw err;
            console.log(result);
            return result[0];
          });

        return handlerInput.responseBuilder
        .speak("Busc " + stat)
        .reprompt("reprompt buscar")
        .getResponse();
}

另一篇文章描述了针对Alexa请求的异步调用。

Hi,感谢您的帮助,我看到了以下日志
16:15:47 2019-12-30T16:15:47.346Z 606a3f88-b17d-499b-bd70-0e5ba140b496[RowDataPacket{spe:777}
当查询运行时,但尝试
.speak(“Busc”+stat.spe)
会使我没有定义[Object]我认为它在数组中是正确的,如果您只有一个元素,然后通过
stat[0].RowDataPacket.spe
访问它,否则您必须迭代该数组并获得所需的值。嘿,我尝试在包“mysql2 promise”中放置“^3.1.1”为了让它工作,但我一直有这个错误,所以我做错了…错误:找不到模块'mysql2 promise'