It's node js. How do I process data synchronously?

Asked 2 years ago, Updated 2 years ago, 41 views

const express = require('express'); const app = express();

var MongoClient = require('mongodb').MongoClient; var url = "mongodb://192.168.0.60:27017/";

MongoClient.connect(url, function(err, db) { if (err) throw err; var dbo = db.db("database"); var cursor = dbo.collection('data').find();

dbo.collection("data").findOne({}, function(err, result) {

  app.get('/', (req, res) => {
  res.json(result);
  console.log(result)

res.end(); db.close(); });
}); });

app.listen(8000, () => { console.log('Example app listening on port 3000!'); });

 I made a data collection like this...

If nodejs is executed, even if the data in mongodb is changed. It doesn't change on localhost:8000.

What should I do? 

node.js

2022-09-21 21:54

1 Answers

dbo.collection ("data") in the callback of app.get.FindOne must be called.


2022-09-21 21:54

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.