最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

无法读取“ for…of”循环中的第一次迭代迭代为空

IT培训 admin 4浏览 0评论

无法读取“ for…of”循环中的第一次迭代/迭代为空

我有一个数组,questionsArray,并且我需要对该数组中的每个问题进行处理。第一次迭代有效,但除此之外,它还说它无法读取null的属性x。这是为什么?-这是遍历数组的代码:

var insertion = new Promise(async (resolve, reject) => {
                    for(const question of questionsArray){
                        await db.query('INSERT INTO `ml-questions-data` (`text`, `status`, `question_id`, `answer_text`, `answer_status`, `answer_date`, `item_id`, `seller_id`, `created`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);',
                         [question.text, question.status, question.id, question.answer.text, question.answer.status, question.answer.date_created, question.item_id, question.seller_id, question.date_created], (erro)=>{
                            if(erro){
                                reject(erro)
                            }
                            else{
                                console.log('array iteration! db insertion')
                            }
                        })
                    }
                    resolve()
                });

-这是questionsArray数据:

[
    {
        "date_created": "2019-09-30T03:18:08.000-04:00",
        "item_id": "MLB1329418290",
        "seller_id": 158369990,
        "status": "ANSWERED",
        "text": "olá, gostaria de entender como funciona isso?",
        "id": 6553335409,
        "deleted_from_listing": false,
        "hold": false,
        "answer": {
            "text": "pois bem, vamos adicionar um texto de resposta no json",
            "status": "ACTIVE",
            "date_created": "2019-09-30T03:24:52.120-04:00"
        },
        "from": {
            "id": 444188609,
            "answered_questions": 2
        }
    },
    {
        "date_created": "2019-09-30T03:22:21.000-04:00",
        "item_id": "MLB1329418290",
        "seller_id": 158369990,
        "status": "BANNED",
        "text": "<p style=\"display: inline-block;font-family:Helvetica,Arial,'sans-serif';font-size:13px;line-height:17px;background: url('.10.7/assets/icons.png') no-repeat;border-radius: 3px;border-style: solid;border-width: 1px;margin: 0 0 5px;padding: 8px 10px 8px 34px;text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);background-color: #FCF8CB;background-position: -225px -101px;border-color: #E4E2B8;\">Tivemos que excluir esta pergunta porque não está de acordo com as nossas <a href=\"\">Políticas para Cadastramento de Anúncios</a>.</p>",
        "id": 6553335807,
        "deleted_from_listing": false,
        "hold": false,
        "answer": null,
        "from": {
            "id": 444188609,
            "answered_questions": 2
        }
    },

    the array goes on.....
]

这是错误节点的提示:

C:\ServidorMaster\Alura\ProjetosNODE\project2>node server
server rodando na porta 3010
Conexão com banco de dados mySQL estabelecida
**(node:9360) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'text' of null
    at Promise (C:\ServidorMaster\Alura\ProjetosNODE\project2\src\app\modelos\DAOs\mercado-livre\ml-DAO.js:205:88)**
(node:9360) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9360) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
**array iteration! db insertion**

因此它说in无法读取该属性,但是第一次迭代(一个对象)已成功插入到我的数据库中。有人可以帮助我了解这里的情况吗?

回答如下:

“ answer”在数组的第二个元素中为空,因此您无法访问question.answer.text。 (在数组的第一个元素中,答案是带有名为“文本”的元素的映射。)

您将需要处理插入中试图访问其成员的所有部分的输入数组中的答案为null的情况。

无法读取“ for…of”循环中的第一次迭代/迭代为空

我有一个数组,questionsArray,并且我需要对该数组中的每个问题进行处理。第一次迭代有效,但除此之外,它还说它无法读取null的属性x。这是为什么?-这是遍历数组的代码:

var insertion = new Promise(async (resolve, reject) => {
                    for(const question of questionsArray){
                        await db.query('INSERT INTO `ml-questions-data` (`text`, `status`, `question_id`, `answer_text`, `answer_status`, `answer_date`, `item_id`, `seller_id`, `created`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);',
                         [question.text, question.status, question.id, question.answer.text, question.answer.status, question.answer.date_created, question.item_id, question.seller_id, question.date_created], (erro)=>{
                            if(erro){
                                reject(erro)
                            }
                            else{
                                console.log('array iteration! db insertion')
                            }
                        })
                    }
                    resolve()
                });

-这是questionsArray数据:

[
    {
        "date_created": "2019-09-30T03:18:08.000-04:00",
        "item_id": "MLB1329418290",
        "seller_id": 158369990,
        "status": "ANSWERED",
        "text": "olá, gostaria de entender como funciona isso?",
        "id": 6553335409,
        "deleted_from_listing": false,
        "hold": false,
        "answer": {
            "text": "pois bem, vamos adicionar um texto de resposta no json",
            "status": "ACTIVE",
            "date_created": "2019-09-30T03:24:52.120-04:00"
        },
        "from": {
            "id": 444188609,
            "answered_questions": 2
        }
    },
    {
        "date_created": "2019-09-30T03:22:21.000-04:00",
        "item_id": "MLB1329418290",
        "seller_id": 158369990,
        "status": "BANNED",
        "text": "<p style=\"display: inline-block;font-family:Helvetica,Arial,'sans-serif';font-size:13px;line-height:17px;background: url('.10.7/assets/icons.png') no-repeat;border-radius: 3px;border-style: solid;border-width: 1px;margin: 0 0 5px;padding: 8px 10px 8px 34px;text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);background-color: #FCF8CB;background-position: -225px -101px;border-color: #E4E2B8;\">Tivemos que excluir esta pergunta porque não está de acordo com as nossas <a href=\"\">Políticas para Cadastramento de Anúncios</a>.</p>",
        "id": 6553335807,
        "deleted_from_listing": false,
        "hold": false,
        "answer": null,
        "from": {
            "id": 444188609,
            "answered_questions": 2
        }
    },

    the array goes on.....
]

这是错误节点的提示:

C:\ServidorMaster\Alura\ProjetosNODE\project2>node server
server rodando na porta 3010
Conexão com banco de dados mySQL estabelecida
**(node:9360) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'text' of null
    at Promise (C:\ServidorMaster\Alura\ProjetosNODE\project2\src\app\modelos\DAOs\mercado-livre\ml-DAO.js:205:88)**
(node:9360) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:9360) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
**array iteration! db insertion**

因此它说in无法读取该属性,但是第一次迭代(一个对象)已成功插入到我的数据库中。有人可以帮助我了解这里的情况吗?

回答如下:

“ answer”在数组的第二个元素中为空,因此您无法访问question.answer.text。 (在数组的第一个元素中,答案是带有名为“文本”的元素的映射。)

您将需要处理插入中试图访问其成员的所有部分的输入数组中的答案为null的情况。

发布评论

评论列表 (0)

  1. 暂无评论