gpt4 book ai didi

javascript - 如何从 get 请求中提取 MongoDB 文档的正文?

转载 作者:行者123 更新时间:2023-11-30 19:10:46 27 4
gpt4 key购买 nike

我正在尝试访问 MongoDB 中 JSON 文档正文中的 Question 字段。当我运行 GET 请求时,我得到如下所示的内容:

{
"_readableState": {
"objectMode": true,
"highWaterMark": 0,
"buffer": {
"head": null,
"tail": null,
"length": 0
},
"length": 0,
"pipes": null,
"pipesCount": 0,
"flowing": null,
"ended": false,
"endEmitted": false,
"reading": false,
"sync": true,
"needReadable": false,
"emittedReadable": false,
"readableListening": false,
"resumeScheduled": false,
"destroyed": false,
"defaultEncoding": "utf8",
"awaitDrain": 0,
"readingMore": false,
"decoder": null,
"encoding": null
},
"readable": true,
"domain": null,
"_events": {},
"_eventsCount": 0,
"_opts": {},
"_destroyed": false
}

我正在阅读有关 JSON 正文解析器的内容,但添加它似乎不起作用。这是我的 index.js:

 var express = require('express')
var mongojs = require('mongojs')
var bodyParser = require("body-parser");
var app = express()
var db = require('./myDB.js')

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(express.json())
app.listen(4000, () => console.log('Hello'))

app.get('/getFlashCard', (req, res) => {
let flashID = req.body._id;
db.getFlashCard("Interview Questions", flashID, function(docs) {
console.log("Flashcard retrieved: ", docs);
res.send(docs);
});
});

这是我的 myDB.js:

getFlashCard : function(colName, flashID, callback) {
let data = mongodb.collection("Interview Questions").find({
"_id" : flashID
});
callback(data);
}

最佳答案

这里您返回的是 promise 模型而不是实际解析的数据。

getFlashCard : async function(colName, flashID, callback) {
const data = await mongodb.collection("Interview Questions").find({_id : flashID });

callback(data);
}

关于javascript - 如何从 get 请求中提取 MongoDB 文档的正文?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58496520/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com