[英]Loop through array of Mongoose data
我正在嘗試從 Mongoose 獲取數據數組,遍歷數組並將 object 添加到 Three.js 場景中的每個項目。
當我嘗試在瀏覽器中渲染場景時,我收到一條錯誤消息:
“client_world.js:37 Uncaught (in promise) TypeError: data is not iterable at getData”
但是,當我控制台記錄查詢數據庫的 get 請求中的數據時,它將所有數據條目打印為一個數組,它們必須是可迭代的。
我想也許數據沒有到達 javascript 文件。
這是從數據庫中獲取數據的代碼:
const indexVR = (req, res) => {
Upload.find({})
// .populate('Upload')
.exec(function (err, docs) {
if (err) res.send(err)
// docs.toString()
res.json(docs)
console.log(docs)
})
};
此 function 在以下獲取請求中:
router.get('/indexvr', FileCtrl.indexVR);
這是來自 js 文件的代碼片段:
getData()
async function getData() {
const response = await fetch('/api/indexvr');
const data = await response.json;
console.log(data)
for (item of data) {
try {
scene.add( cube );
} catch (e) {
console.error(e)
}
}
}
在 getData function 中登錄控制台沒有任何內容,但是瀏覽器正在讀取某種空的 json object。
..有誰知道我做錯了什么?
您的問題可能就像調用.json
方法而不是引用它一樣簡單。
參考這個文檔: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch .json
是響應fetch
返回的方法。
嘗試使用const data = await response.json();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.