Node.js NodeJS MongoDB Mongoose-获取新创建架构的.id

Node.js NodeJS MongoDB Mongoose-获取新创建架构的.id,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我有一个应用程序,允许用户对书籍进行评分。这些书是从GoogleBooksAPI调用的。当用户提交他们的评分时,我只在我的数据库中保存一份书的副本 reviews.put("/:id/new", async (req, res) => { let url = await `https://www.googleapis.com/books/v1/volumes/${req.params.id}`; console.log(url); await request(url, { js

我有一个应用程序,允许用户对书籍进行评分。这些书是从GoogleBooksAPI调用的。当用户提交他们的评分时,我只在我的数据库中保存一份书的副本

reviews.put("/:id/new", async (req, res) => {
  let url = await `https://www.googleapis.com/books/v1/volumes/${req.params.id}`;
  console.log(url);
  await request(url, { json: true }, async (error, response, data) => {
    let newRating;
    Book.findOne({ id: req.params.id }, (err, result) => {
      if (err) console.log(err.message);
      if (result) {
        if (req.body.stars !== undefined) {
          newRating = /* some math formula */
        } else {
          newRating = /* some math formula */
        }
      } else {
        newRating = req.body.stars;
      }
    });
    Book.findOneAndUpdate(
      {
        id: req.params.id
      },
      {
        id: data.id,
        id: data.id,
        title: data.volumeInfo.title,
        description: data.volumeInfo.description,
        img: data.volumeInfo.imageLinks.thumbnail,
        author: data.volumeInfo.authors,
        rating: newRating,
        $inc: {
          ratingCount: 1
        }
      },
      {
        upsert: true,
        returnNewDocument: true
      },
      (err, book) => {
        console.log(book) // If its creating a new document, book returns null. If the book is already in the DB, book returns the document. 
        Review.create({
          rating: req.body.stars,
          review: req.body.review,
          reviewer: req.session.currentUser._id,
          book: book._id // <-- ERROR, cannot read property "_.id" of null
        });
      }
    );
  });

  res.redirect("/");
});
reviews.put(“/:id/new”),异步(req,res)=>{
让url=等待`https://www.googleapis.com/books/v1/volumes/${req.params.id}`;
console.log(url);
等待请求(url,{json:true},异步(错误、响应、数据)=>{
让新评级;
Book.findOne({id:req.params.id},(err,result)=>{
if(err)console.log(err.message);
如果(结果){
if(req.body.stars!==未定义){
newRating=/*一些数学公式*/
}否则{
newRating=/*一些数学公式*/
}
}否则{
newRating=所需的body.stars;
}
});
Book.findOneAndUpdate(
{
id:req.params.id
},
{
id:data.id,
id:data.id,
标题:data.volumeInfo.title,
description:data.volumeInfo.description,
img:data.volumeInfo.imageLinks.缩略图,
作者:data.volumeInfo.authors,
评级:新评级,
$inc:{
评级计数:1
}
},
{
厄普塞特:没错,
returnNewDocument:true
},
(呃,书)=>{
console.log(book)//如果它正在创建一个新文档,book将返回null。如果该书已经在数据库中,book将返回该文档。
查看。创建({
额定值:要求的体星,
审查:要求主体审查,
审核人:请求会话.currentUser.\u id,

book:book.\u id/使用
'new':true
如下:
{upsert:true,'new':true}


这将返回上传的文档。

您通过的是错误的。您需要使用:

new
:bool-如果为true,则返回修改后的文档,而不是原始文档

upsert
:bool-如果对象不存在,则创建该对象。默认值为false

更新方法如下:

Book.findOneAndUpdate(
      {
        id: req.params.id
      },
      {
        id: data.id,
        id: data.id,
        title: data.volumeInfo.title,
        description: data.volumeInfo.description,
        img: data.volumeInfo.imageLinks.thumbnail,
        author: data.volumeInfo.authors,
        rating: newRating,
        $inc: {
          ratingCount: 1
        }
      },
      {
        upsert: true,
        new: true
      },
      (err, book) => {
        console.log(book) // If its creating a new document, book returns null. If the book is already in the DB, book returns the document. 
        Review.create({
          rating: req.body.stars,
          review: req.body.review,
          reviewer: req.session.currentUser._id,
          book: book.id // <-- ERROR, cannot read property "_.id" of null
        });
      }
Book.findOneAndUpdate(
{
id:req.params.id
},
{
id:data.id,
id:data.id,
标题:data.volumeInfo.title,
description:data.volumeInfo.description,
img:data.volumeInfo.imageLinks.缩略图,
作者:data.volumeInfo.authors,
评级:新评级,
$inc:{
评级计数:1
}
},
{
厄普塞特:没错,
新:真的
},
(呃,书)=>{
console.log(book)//如果它正在创建一个新文档,book将返回null。如果该书已经在数据库中,book将返回该文档。
查看。创建({
额定值:要求的体星,
审查:要求主体审查,
审核人:请求会话.currentUser.\u id,
book:book.id//