我正在嘗試用來自 unsplash 和 randomuser api 的資料填充我的 mongodb 檔案。
const userdata = await axios.get("https://randomuser.me/api/?results=51");
const imagedat = await axios.get(
"https://api.unsplash.com/photos/random/?count=51&client_id=GWDzPpjHk743C2QnVBRxu8PtmOI3npF5sePZZ7o0pg4"
);
我為 51 個結果呼叫了這兩個 api,但是在 22 個結果之后,catch 下的代碼被顯示,并且只創建了 22 個檔案
如何存盤所有 51 個結果
const seeddata = async () => {
const userdata = await axios.get("https://randomuser.me/api/?results=51");
const imagedat = await axios.get(
"https://api.unsplash.com/photos/random/?count=51&client_id=GWDzPpjHk743C2QnVBRxu8PtmOI3npF5sePZZ7o0pg4"
);
try {
await MemoModel.deleteMany({});
const userarr = await userdata.data;
const imagedata = await imagedat.data;
for (let i = 0; i < 50; i ) {
const data = new MemoModel({
name: {
firstname: `${userarr.results[i].name.first}`,
lastname: `${userarr.results[i].name.last}`,
username: `${userarr.results[i].login.username}`,
},
about: {
user: `${userarr.results[i].gender} aged ${userarr.results[i].dob.age}. Rotting here for ${userarr.results[i].registered.age} `,
},
location: {
country: `${
countryarr[Math.floor(Math.random() * countryarr.length)]
}`,
state: `${userarr.results[i].location.state}`,
city: `${userarr.results[i].location.city}`,
address: `${userarr.results[i].location.street.name} ,${userarr.results[i].location.street.number}`,
zipcode: `${userarr.results[i].location.postcode}`,
},
email: {
user: `${userarr.results[i].email}`,
},
image: {
dp: `${userarr.results[i].picture.large}`,
coverimage: "https://source.unsplash.com/random/?mountains",
},
posts: {
postno: i,
posttitle: `${imagedata[i].description}`,
postcontent: `${imagedata[i].urls.regular}`,
postlikesno: imagedata[i].likes,
postcommentno: imagedata[i].width,
},
});
await data.save();
}
} catch {
console.log("catastrophic Failure");
}
};
seeddata().then(() => {
mongoose.connection.close();
});

uj5u.com熱心網友回復:
您可以(err)在您的catch和控制臺之后添加它而不是“災難性錯誤”嗎?它會給我們整個錯誤,破壞你的代碼:
catch(err) {
console.error(err)
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/489697.html
下一篇:NodeJs回呼和引數
