我有一个node.js服务器来获取我的帖子数据。 基本上它使用过滤器和订单获取集合中的所有文档:
var ref = db.collection('posts');
var visible = request.query.visible;
var query = null;
if(visible === true) {
query = ref.where('visible', '==', true)
.orderBy('creation_date', 'desc');
}
else {
query = ref.orderBy('creation_date', 'desc');
}
query.get()
.then(res => {
result.send(res);
})
.catch(err => {
result.status(500).send(err);
});
在客户端,我向服务器发出Ajax请求并循环收到的项目。
在firestore类中使用Ajax Request GetPosts()函数
var firebaseApiRequest = AjaxRequest(`${this.apiUrl}/posts`, 'GET');
firebaseApiRequest.send();
firebaseApiRequest.onreadystatechange = function () {
if(this.readyState === 4) {
if(this.status === 200) resolve(JSON.parse(this.responseText));
else {
resolve([]);
}
}
}
index.js函数显示我从服务器收到的帖子
let firestoreAPI = new firestore();
firestoreAPI.GetPosts().then(posts => {
posts.forEach(post => {
console.log(post);
let json = post.data();
json.id = post.id;
postsList.push(json);
});
BuildPosts();
});
在node.js服务器上工作正常,我收到了json的帖子。当代码到达 posts.forEach 时,我收到此错误:
未捕获(承诺)TypeError:posts.forEach不是函数
这是我从服务器回来的Json(我删除了一些敏感数据)
{
"_query": {
"_referencePath": {
"segments": ["posts"]
},
"_fieldFilters": [],
"_fieldOrders": [{
"_field": {
"segments": ["creation_date"],
"_formattedName": "creation_date"
},
"_direction": "DESCENDING"
}],
"_queryOptions": {}
},
"_readTime": "2018-01-27T18:50:36.587401000Z",
"_materializedDocs": [{
"_fieldsProto": {
"clicks": {
"integerValue": "6",
"valueType": "integerValue"
},
"title": {
"stringValue": "Corso gratuito sviluppo giochi con Unity 3D",
"valueType": "stringValue"
},
"creation_date": {
"integerValue": "1516813022363",
"valueType": "integerValue"
},
"author": {
"stringValue": "xcoding.it",
"valueType": "stringValue"
},
"url": {
"stringValue": "https://www.xcoding.it/corso/le-basi-di-unity-3d/",
"valueType": "stringValue"
},
"creation_user": {
"stringValue": "marcodz554@gmail.com",
"valueType": "stringValue"
},
"edit_date": {
"integerValue": "1516871809344",
"valueType": "integerValue"
},
"edit_user": {
"stringValue": "marcodz554@gmail.com",
"valueType": "stringValue"
},
"visible": {
"booleanValue": true,
"valueType": "booleanValue"
},
"text": {
"stringValue": "Corso base per i principianti con Unity3D che ricopre la maggior parte dei punti per scoprire questo engine. Completamente in lingua italiana e ben redatto, unica pecca la mancanza di video-guide annesse.",
"valueType": "stringValue"
},
"type": {
"stringValue": "corso",
"valueType": "stringValue"
}
},
"_readTime": "2018-01-27T18:50:36.587401000Z",
"_createTime": "2018-01-24T09:01:41.929842000Z",
"_updateTime": "2018-01-26T14:15:31.739188000Z"
}, {
"_fieldsProto": {
"creation_date": {
"integerValue": "1516812951013",
"valueType": "integerValue"
},
"author": {
"stringValue": "brackeys.com",
"valueType": "stringValue"
},
"url": {
"stringValue": "https://www.youtube.com/user/Brackeys",
"valueType": "stringValue"
},
"creation_user": {
"stringValue": "marcodz554@gmail.com",
"valueType": "stringValue"
},
"visible": {
"booleanValue": true,
"valueType": "booleanValue"
},
"text": {
"stringValue": "Canale Youtube rifornito ogni due giorni circa con video inerenti a Unity3D. Ottime spiegazioni e facile da capire con una conoscenza base della lingua inglese.",
"valueType": "stringValue"
},
"type": {
"stringValue": "canale",
"valueType": "stringValue"
},
"clicks": {
"integerValue": "4",
"valueType": "integerValue"
},
"title": {
"stringValue": "Brackeys",
"valueType": "stringValue"
}
},
"_readTime": "2018-01-27T18:50:36.587401000Z",
"_createTime": "2018-01-24T12:06:00.792552000Z",
"_updateTime": "2018-01-25T11:04:57.606426000Z"
}]
}
答案 0 :(得分:1)
鉴于您展示的JSON,它确实在您返回的对象上没有forEach
。
我看到的唯一数组是_materializedDocs
,您可以使用以下代码循环:
posts._materializedDocs.forEach(...)
答案 1 :(得分:0)
我解决了在服务器端使用docs DATA和ID构建自定义Json。