简体   繁体   English

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

[英]How do I extract the body out of the MongoDB Document from my get request?

I am trying to access the Question field in the body of a JSON document inside a MongoDB.我正在尝试访问 MongoDB 内的 JSON 文档正文中的Question字段。 When I run the GET request I get something that looks like this:当我运行 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
}

I was reading about JSON body parsers but adding that does not seem to be working.我正在阅读有关 JSON 正文解析器的信息,但补充说这似乎不起作用。 Here is my index.js :这是我的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);
    });
 });

Here is my myDB.js :这是我的myDB.js

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

here you are returning the promise model instead of the actual resolved data.在这里,您将返回 promise model 而不是实际解析的数据。

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

   callback(data);
}

Notice that you are not waiting for the find to finish, instead you are assigning data to mongodb.collection("").find请注意,您不是在等待find完成,而是将data分配给mongodb.collection("").find

You need to wait for find to finish by either using a callback (check out https://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find ) or by using async/await.您需要通过使用回调(查看https://mongodb.github.io/node-mongodb-native/api-generated/collection.html#find )或使用 async/await 来等待find完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Get Request In Express 中获取正文? - How Do I Get Body From Get Request In Express? 如何获取要从请求正文发送的文件作为参数? - How do I get the file to be sent from the request body as a parameter? 如何在 Moleculer 中获取请求正文 - How do I get request body in Moleculer 在Postman中,我如何从“获取请求”中获取响应正文,并将其放入经过细微更改的PUT请求中 - In Postman, how do I take a response body from a Get Request, and put it in a PUT request with minor changes 如何使用 javascript 从 MongoDB 获取一个带有 ID 的文档 - How do I get one document from MongoDB with ID from URL using javascript 如何在NodeJs中用express从请求中提取正文? - How to extract body from request with express in NodeJs? 我如何从身体获得输入 - How do i get input from body 如何将 POST 请求中的数据保存为变量,以在 GET 请求中使用? - How do I save my data from my POST request, as a variable, to use in my GET request? 如何从我的计算器中删除 document.write() - How do I delete document.write() out of my calculator 我希望将 javascript 中该函数的函数名称和主体解析为一个单独的文件。 我如何使用 Java 做到这一点? - I want the function name and body of that function from a javascript to get parsed out into a separate file. How do I do that using Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM