我目前正在使用Firebase和Firebase功能。我正在尝试在集合(集合:“项目”)中获取文档。我尝试进行一次GET,但是我只有200个空的车身。
fun changeUserAccount(
userName: String,
password: String,
confirmPassword: String,
realName: String,
accountEmail: String,
applicationContext: Context
) {
//You can pass n number of strings.
val isAllStringsValid = StringUtils.isAllValid(userName,password,confirmPassword,realName,accountEmail)
//this returns true
StringUtils.isAllValid("hi","how","are","you","?")
//this returns false
StringUtils.isAllValid("","how","are","you","?")
//this returns false
StringUtils.isAllValid(null,"how","are","you","?")
//this returns false
StringUtils.isAllValid("","how","are","you","?")
}
有趣的是,当我查询特定项目时,我得到了包含文档数据的JSON正文:
app.get('/api/read/items', (req, res) => {
(async () => {
try {
let response = [];
let itemRefs = db.collection('items').get().then(snapshot => {
snapshot.forEach((item) => {
response.push(item.data());
});
console.log(response);
});
return res.send(response);
} catch (error) {
debug.log(error);
return res.status(500).send(error);
}
})();
});
期待看到我犯了什么错误:)!
谢谢您的帮助!
答案 0 :(得分:1)
数据从Firestore异步加载。到您的res.send(response)
执行时,snapshot.forEach(...)
尚未运行。
解决方案是从最内层的回调中将响应发送到客户端:
app.get('/api/read/items', (req, res) => {
(async () => {
try {
let response = [];
return db.collection('items').get().then(snapshot => {
snapshot.forEach((item) => {
response.push(item.data());
});
console.log(response);
return res.send(response);
});
} catch (error) {
debug.log(error);
return res.status(500).send(error);
}
})();
});
或者,您可以像在第二个片段中一样使用await
:
app.get('/api/read/items', (req, res) => {
(async () => {
try {
let response = [];
let snapshot = await snapshot db.collection('items').get();
snapshot.forEach((item) => {
response.push(item.data());
});
return res.send(response);
} catch (error) {
return res.status(500).send(error);
}
})();
});
您可以进一步简化以下内容:
app.get('/api/read/items', (req, res) => {
(async () => {
try {
let snapshot = await snapshot db.collection('items').get();
let response = snapshot.documents.map((item) => item.data());
return res.send(response);
} catch (error) {
return res.status(500).send(error);
}
})();
});
答案 1 :(得分:0)
我想我已经找到了问题的根本原因,由于我对节点/表达式的了解不足,所以我无法解释为什么。 在测试时,我有index.js文件,其中包含:
public class MoviePostersListViewModel extends ViewModel {
MovieRepository movieRepository;
public MoviePostersListViewModel() {
movieRepository = MovieRepository.getInstance();
}
public LiveData<List<Movie>> getMovies() {
return movieRepository.getMovies();
}
public void retrieveMoviesApi(String query, int pageNumber){
movieRepository.retrieveMoviesApi(query, pageNumber);
}
}
尝试对read / geoFencedUsers进行GET调用会返回200,但响应为空。 更改完我的index.js并在上面移动了get方法后,解决了使所有3个调用都工作以返回正确的数据响应的问题。
const serviceAccount = require("./permissions.json");
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const express = require('express');
const cors = require('cors');
const app = express();
const db = admin.firestore();
......
app.get('/api/read/items', (req, res) => {
(async () => {
try {
let response = [];
return db.collection('items').get().then(snapshot => {
snapshot.forEach((item) => {
response.push(item.data());
});
console.log(response);
return res.send(response);
});
} catch (error) {
console.log(error);
return res.status(500).send(error);
}
})();
});
// read item
app.get('/api/read/:item_id', (req, res) => {
(async () => {
try {
const document = db.collection('items').doc(req.params.item_id);
let item = await document.get();
let response = item.data();
return res.status(200).send(response);
} catch (error) {
console.log(error);
return res.status(500).send(error);
}
})();
});
app.get('/api/read/geoFencedUsers', (req, res) => {
(async () => {
try {
let response = [];
return db.collection('geoFencedUsers').get().then(snapshot => {
snapshot.forEach((geoFencedUser) => {
response.push(geoFencedUser.data());
});
return res.status(200).send(response);
})
} catch (error) {
return res.status(500).send(error);
}
})();
});