Javascript 在mongoose Atlas中保存数据时通过GET方法发送数据
我有一个问题:) 我的问题是,在从GET方法发送数据库信息之前,如何等待信息保存在Mongoose数据库中。更多的数据保存在POST方法中,GET方法在POST方法之外完成 问题在于GET方法中的console.log出现在console.log之前,用于检查数据是否保存 这是我的密码:Javascript 在mongoose Atlas中保存数据时通过GET方法发送数据,javascript,post,mongoose,promise,get,Javascript,Post,Mongoose,Promise,Get,我有一个问题:) 我的问题是,在从GET方法发送数据库信息之前,如何等待信息保存在Mongoose数据库中。更多的数据保存在POST方法中,GET方法在POST方法之外完成 问题在于GET方法中的console.log出现在console.log之前,用于检查数据是否保存 这是我的密码: // Variables needed to access the API const API_url = "https://www.googleapis.com/books/v1/volumes?
// Variables needed to access the API
const API_url = "https://www.googleapis.com/books/v1/volumes?q=isbn:";
const API_key = process.env.GOOGLE_BOOKS_APIKEY;
// Connection to the Google Books API to retrieve dataObject
app.post('/server', async(req,res)=>{
// receive input send by client side (used it to access pictures from google API and also )
dataObject = req.body;
const response_books_api = await fetch(`${API_url}${dataObject.book_entry.isbn}&key=${API_key}`);
try {
const json_books_api = await response_books_api.json();
console.log(json_books_api);
res.send(json_books_api);
console.log(json_books_api.items[0].volumeInfo.imageLinks.thumbnail);
// Save data from the userInput in Mongoose Atlas database
const books = new Books({
book_name: dataObject.book_entry.book_name,
subtitle: dataObject.book_entry.subtitle,
author: dataObject.book_entry.author,
publishing_date: dataObject.book_entry.publishing_date,
publishing_house: dataObject.book_entry.publishing_house,
isbn: dataObject.book_entry.isbn,
collection_data: dataObject.book_entry.collection,
description: dataObject.book_entry.description,
});
const images = new Images({
imageUrl: json_books_api.items[0].volumeInfo.imageLinks.thumbnail,
})
books.save().then(()=>{
console.log('data successfully posted!')
// res.status(201).json({
// message: 'data successfully posted!',
// });
}).catch((error)=>{
console.log(error);
// res.status(400).json({
// error: error,
// })
});
function send_image(){
images.save().then(()=>{
console.log('data successfully posted!')
// res.status(201).json({
// message: 'data successfully posted!',
// });
}).catch((error)=>{
console.log(error);
// res.status(400).json({
// error: error,
// })
});
}
} catch (err) {
console.error(err);
}
});
// send the book information from database to get it on the client side
app.get('/server/books',(req,res)=>{
Books.find().then(
(books) =>{
res.status(200).json(books);
}
).catch(
(error) =>{
console.log(error);
res.statut(400).json({
error: error
});
}
);
});
// send the imageURL from database to get it on the client side
app.get('/server/images',(req,res)=>{
Images.find().then(
(images) =>{
res.status(200).json(images);
console.log('ok send')
}
).catch(
(error) =>{
console.log(error);
res.statut(400).json({
error: error
});
}
);
})
没人帮我,嗨,穆罕默德,我看到了你的另一个问题,关于没有得到帮助。我会尽力帮你的。我觉得你的问题太难理解了,所以很少有人看。您能告诉我您正在发出哪些GET和POST请求,以及如何在前端调用它们吗?示例post数据也会很有用。然后,我将尝试帮助您重新格式化问题并回答它。