I'm following a tutorial and I can see the correct result from the console.log(result)
in the console, but when I make the GET request from postman, it returns nothing. Why is send(200, JSON.parse(JSON.stringify(result)));
not working?
I also did a console.log(JSON.parse(JSON.stringify(result)))
and it returns correctly the data correctly to the console. Just can't send it in response for some reason.
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();
};
}
I am not sure what "send" function does in this case, but I think if you just change it to the following should work:
db.collection('listings').find({}) //test
.toArray((err, result) => {
console.log(result);
if (err)
{
context.res = { status: 500, body: err.message };
}
else
{
context.res = { status: 200, body: JSON.parse(JSON.stringify(result)) };
}
context.done();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.