我使用 Cloud Firestore 的方式如下:
“事件”集合包含使用唯一事件 ID 作为名称的文档。这些文档中有许多“EventComment”对象 - 每个对象代表用户所做的评论。
要将“EventComment”对象添加到文档中,我使用以下命令:
EventComment mcomment = new EventComment();
mcomment.setComment("this is a comment");
Map<String, EventComment> eventMap = new HashMap<>();
eventMap.put(Long.toHexString(Double.doubleToLongBits(Math.random())), mcomment);
firestore.collection("events").document(event_id)
.set(eventMap, SetOptions.merge()).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
Toast.makeText(EventCommentsActivity.this, "ya bastud", Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud)
我创建一个HashMapString 和我的对象“EventComment”,然后在文档中设置它。
但是,当我想检索给定文档中包含的所有“EventComment”对象时,我无法将其转换为 EventComment 对象,即我无法执行此操作:
DocumentReference docRef = db.collection("events").document("vvG17ZfcLFVna8");
docRef.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
EventComment comment = documentSnapshot.toObject(EventComment.class);
}
});
Run Code Online (Sandbox Code Playgroud)
此外,尝试将 documentSnapshot 转换为 HashMap 会给出“未经检查的转换”消息,并最终失败。这个想法是使用注释来填充 RecyclerView。
我在想我可能需要重组存储数据的方式?
任何建议将不胜感激。
干杯
我正在使用 Cypress 进行一些 API 测试,但我很难访问 JSON 响应正文中的值;但是我可以对身体进行断言,这表明它正确地接收了它。
下面我试图分配 JSON 正文(response.body),然后从中获取 'id' 的值:
describe('Creating a board', () => {
it('should create a board', () => {
cy.request({
method : 'POST',
url:`${requestUrl}/boards/`,
qs: {
name : "test-board",
token : token,
key : key
}
}).then((response) => {
expect(response).property('status').to.equal(200)
expect(response.body).property('id').to.not.be.oneOf([null, ""])
const body = (response.body)
boardId = body['id']
})
})
Run Code Online (Sandbox Code Playgroud)
我已经做了很多搜索,但找不到具体的方法来做到这一点。任何帮助,将不胜感激...