Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.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
Javascript Can';t在“中”的第一次迭代之后读取;为了……的;循环/迭代为空_Javascript_Node.js_For Loop - Fatal编程技术网

Javascript Can';t在“中”的第一次迭代之后读取;为了……的;循环/迭代为空

Javascript Can';t在“中”的第一次迭代之后读取;为了……的;循环/迭代为空,javascript,node.js,for-loop,Javascript,Node.js,For Loop,我有一个数组,questionsArray,我需要在数组中的每个问题中做一些事情。第一次迭代可以工作,但除此之外,它还表示无法读取null的属性x。为什么呢? -这是在数组中循环的代码: var insertion = new Promise(async (resolve, reject) => { for(const question of questionsArray){ await db.quer

我有一个数组,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()
                });
-以下是问题,请参阅数据:

[
    {
        "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('http://static.mlstatic.com/org-img/ch/ui/0.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=\"https://www.mercadolivre.com.br/ajuda/politicas-para-cadastramento-de-produtos_1002\">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.....
]
所以它说in无法读取属性,但是第一次迭代,一个对象,成功地插入到了我的数据库中。有人能帮我理解这里发生了什么吗

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

您将需要处理这样的情况,即对于尝试访问其成员的insert的所有部分,输入数组中的answer都为null。

进行检查

question.answer?question.answer.text : ""
所以基本上这是对应答对象的空检查。所以,如果答案不是空的,那么它只会执行question.answer.text


否则它将给出/分配“”空值

非常感谢您检查空值后您的问题仍然存在吗?不,现在已修复。非常感谢你帮助我。这是我的第一个“大”项目,我日以继夜地工作。虽然这段代码片段可能是解决方案,但它确实有助于提高你文章的质量。请记住,您将在将来回答读者的问题,而这些人可能不知道您的代码建议的原因。如果使用此解决方案,您还将遇到以下问题:question.answer被多次引用(question.answer.status位于question.answer.text之后)因此,您需要解决所有引用question.answer的地方。
question.answer?question.answer.text : ""