【发布时间】:2020-03-09 19:03:46
【问题描述】:
我正在关注tutorial,我可以从控制台中的console.log(result) 看到正确的结果,但是当我从邮递员发出 GET 请求时,它什么也没返回。为什么是send(200, JSON.parse(JSON.stringify(result)));
不工作?
我还做了一个console.log(JSON.parse(JSON.stringify(result))),它正确地将数据正确返回到控制台。只是由于某种原因无法发送响应。
var MongoClient = require('mongodb').MongoClient;
var Post = require('./model/post');
module.exports = async function (context, req) {
let currentPage = 1;
context.log("process.env.CosmosDBConnectionString");
context.log(process.env.CosmosDBConnectionString);
MongoClient.connect(process.env.CosmosDBConnectionString, (err, client) => {
let send = response(client, context);
if (err) send(500, err.message);
console.log("DBNAME: " + process.env.dbName);
let db = client.db(process.env.dbName);
let queryDate = Date.now();
db.collection('listings').find({}) //test
.toArray((err, result) => {
console.log(result);
if (err) send(500, err.message);
send(200, JSON.parse(JSON.stringify(result)));
});
});
};
function response(client, context) {
return function (status, body) {
context.res = {
status: status,
body: body
};
client.close();
context.done();
};
}
【问题讨论】:
标签: node.js mongodb azure mongoose azure-functions