简体   繁体   English

javascript object中的function怎么处理?

[英]How to handle function in javascript object?

I'm trying to fetch data inside object, but my object shows whole function.我试图在 object 中获取数据,但我的 object 显示整个 function。 Why doesn't it show only my return statement?为什么它不只显示我的退货声明? What's the way to handle this problem?处理这个问题的方法是什么?

const fetchContacts = async () => {
    try {
        await axios.get(url, {
           headers: {
              'auth': 'asdsdfasfd'
           }
        }).then(resp => { 
            const newRows = resp && resp.data && resp.data.map(row => 
                ({
                    name: row.name,
                    surname: row.surname,
                    city: row.city,
                    familyNumber: async () => {
                        try {
                            const resp = await axios.get(url, {
                                headers: {
                                    'auth': 'asdsdfasfd'
                                }
                            })
                            return resp.data.length
                        } catch(error) {
                            return error
                        }
                    },
                })
            )
        })
    } catch(error) {
        return error
    }
}

You are using async/await .您正在使用async/await You don't have to use the promise returned by .then .您不必使用 .then 返回的.then Try something like this.尝试这样的事情。 I haven't tested the code.我没有测试过代码。

const fetchContacts = async () => {
    try {

       const resp =  await axios.get(url, {
           headers: {
              'auth': 'asdsdfasfd'
           }
        });

        const resNo = await axios.get(url2, {
                                headers: {
                                    'auth': 'somethingNew-qednken'
                                }
                            });


         const newRows = resp.data && resp.data.map(row => 
                ({
                    name: row.name,
                    surname: row.surname,
                    city: row.city,
                    familyNumber: resNo.data.length || 0
                })
            );

        //console.log("my newRows : ", newRows);

    } catch(err) {
        console.log(err);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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