问题是api请求中的数据在数据库中的字段应显示为“ 0个字段”。我认为这是因为mongo在api响应位于对象变量之前将对象插入数据库。所以我认为我需要让数据库插入功能等待api请求完成。
有人知道我该怎么做吗?
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const fs = require('fs');
const fetch = require('node-fetch');
var mongodb = require('mongodb')
var mongoDbQueue = require('mongodb-queue')
const url = 'mongodb://localhost:27017/'
const client = new mongodb.MongoClient(url, { useNewUrlParser: true })
const app = express();
// View engine setup
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.set('views', __dirname);
// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.render('main');
});
app.post('/send', (req, res) => {
var item = {
name: req.body.name,
age: req.body.age,
country: req.body.country,
isValid:
fetch(*/normaly working url inside here*/)
.then(res => res.json())
.then(data => isValid = data)
.then(() => console.log(isValid))
}
client.connect(err => {
const db = client.db('test')
const queue = mongoDbQueue(db, 'my-queue')
queue.add(item, (err, id) => {
})
})
});
app.listen(3000, () => console.log('Server started...'));
.then(() => console.log(equivalent))
为我提供了正确的值,因此与api无关
答案 0 :(得分:0)
问题出在提取API的异步特性上。直到稍后的某个时间点,isValid中的值才被解析。另外,您使用的值有误。
下面的代码等待响应,设置值,并在正确的范围内更新数据库。
app.post('/send', (req, res) => {
fetch(*/normaly working url inside here*/)
.then(res => res.json())
.then(isValid => {
var item = {
name: req.body.name,
age: req.body.age,
country: req.body.country,
isValid //same as isValid : isValid
}
client.connect(err => {
if(!err) { // error check
const db = client.db('test');
const queue = mongoDbQueue(db, 'my-queue');
queue.add(item, (err, id) => {})
}
});
});
});
答案 1 :(得分:0)
目前尚不清楚您要达到的目标, 但我的猜测是,您希望在提取时添加数据库条目 Promise已在/ send路线上完成。在这种情况下,您需要 像这样重构代码:
const express = require('express');
const bodyParser = require('body-parser');
const exphbs = require('express-handlebars');
const path = require('path');
const fs = require('fs');
const fetch = require('node-fetch');
var mongodb = require('mongodb')
var mongoDbQueue = require('mongodb-queue')
const url = 'mongodb://localhost:27017/'
const client = new mongodb.MongoClient(url, { useNewUrlParser: true })
const app = express();
// View engine setup
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
app.set('views', __dirname);
// Static folder
app.use('/public', express.static(path.join(__dirname, 'public')));
// Body Parser Middleware
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.get('/', (req, res) => {
res.render('main');
});
app.post('/send', (req, res) => {
let name = req.body.name;
let age = req.body.age;
let country = req.body.country;
fetch( /* normaly working url inside here */)
.then(res => res.json())
.then(data => {
let item = { name, age, country, isValid: true };
// data is ready to be used or added to the database at this point:
client.connect(err => {
const db = client.db('test')
const queue = mongoDbQueue(db, 'my-queue')
queue.add(item, (err, id) => {
})
})
})
.catch((error) => console.log(error));
});
app.listen(3000, () => console.log('Server started...'));