【发布时间】:2018-01-13 22:22:52
【问题描述】:
我正在为我的 android 应用程序使用 Firebase 数据库。我正在使用它的云功能特性。我已经编写了函数并尝试在本地机器上对其进行测试。
我正在运行这个命令来测试它,云功能在 Firebase 服务器上运行良好,而不是在 localhost 上。以下命令发送错误
firebase serve --only 函数
> firebase serve --only functions
=== Serving from 'D:\Firebase'...
i functions: Preparing to emulate HTTPS functions. Support for other event types coming soon.
Warning: You're using Node.js v7.10.0 but Google Cloud Functions only supports v6.11.1.
Server#addProtoService is deprecated. Use addService instead
+ functions: getUsers: http://localhost:5002/xyz/us-central1/getUsers
info: User function triggered, starting execution
info: Execution took 60022 ms, finished with status: 'timeout'
info: Execution took 60022 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at Socket.socketErrorListener (_http_client.js:358:9)
info: Execution took 60054 ms, finished with status: 'crash'
Error: Can't set headers after they are sent.
at ServerResponse.setHeader (_http_outgoing.js:371:11)
at ServerResponse.header (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:730:10)
at ServerResponse.send (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:170:12)
at ServerResponse.json (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:256:15)
at ProxyServer.Supervisor._proxy.on (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\@google-cloud\functions-emulator\src\supervisor\supervisor.js:97:12)
at ProxyServer.emit (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\eventemitter3\index.js:144:27)
at ClientRequest.proxyError (C:\Users\williams\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:156:18)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:191:7)
at Socket.socketCloseListener (_http_client.js:334:9)
代码:
exports.getUsers = functions.https.onRequest((request, response) => {
var x = [],
similarityCount;
db
.ref("/users/" + request.query.userId)
.once("value")
.then(function(snapshot) {
var jsonObject = snapshot.val();
var basicProfileJsonObject = jsonObject.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
x.push(basicProfileJsonObject[key]);
}
}
db.ref("/users/").once("value").then(function(snapshot) {
var y = [];
var jsonResponse = [];
snapshot.forEach(function(item) {
var user = item.val();
let userId = user.basicProfile.userId;
if (userId !== request.query.userId) {
var basicProfileJsonObject = user.basicProfile;
for (var key in basicProfileJsonObject) {
if (utils.isNumber(basicProfileJsonObject[key])) {
y.push(basicProfileJsonObject[key]);
}
}
if (request.query.algo === "cosine") {
// compute cosine value
similarityCount = cosineUtils.cosineSimilarity(x, y);
} else if (request.query.algo == "euclidean") {
// compute euclidean distance value
similarityCount = 1 / (1 + euclidean(x, y));
} else if (request.query.algo === "pearson-correlation") {
// compute pearson correlation coefficents
similarityCount = pcorr.pearsonCorrelation(x, y);
}
console.log("------------------------------------");
console.log(x);
console.log("------------------------------------");
console.log("------------------------------------");
console.log(y);
console.log("------------------------------------");
console.log("------------------------------------");
console.log(similarityCount);
console.log("------------------------------------");
jsonResponse.push(item.val());
y = [];
}
});
response.send(jsonResponse);
});
});
});
谁能帮我看看怎么回事?
【问题讨论】:
-
这个错误通常发生在两次回调调用中。检查您的代码是否有错误:
function (err, res) { if (err) return ...}. -
@AikonMogwai 它在firebase服务器上工作而不是在本地机器上,这有意义吗?
-
@AikonMogwai 我添加了有问题的代码,你现在可以检查一下吗
-
我没有看到任何错误处理。没有它们的代码总是有奇怪的行为。在这种情况下,我认为您没有连接到 firebase(检查连接参数)并且
df.ref被浪费了。 -
此错误表示您在关闭响应后正在写入响应。在您的情况下,您似乎多次致电
response.send(jsonResponse)。另请参阅我的答案:stackoverflow.com/questions/14243100/…。我必须承认,虽然我并没有立即看到你是如何多次调用send()的。
标签: node.js firebase firebase-realtime-database firebase-tools