最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

使用html ids查询mongoDB文档

IT培训 admin 5浏览 0评论

使用html ids查询mongoDB文档

我一直在学习使用mongoDB,Express,JQuery和Node.js构建CRUD应用程序的教程。我现在正在尝试添加自定义功能,当我偏离我模仿的代码时,我无法编辑和检索文档。我很好奇发生了什么。我已经按照删除和编辑功能工作,构建一个新的编辑功能,允许我更新多个字段和不同的用户界面。我还想让文档使用这些字段来填充网站不同部分的各种块的HTML,但是我没有让文档返回。

我确信get和put函数在Postman中有效。

这是什么工作:

app.put('/:id',(req,res) =>{
  const todoID = req.params.id;
  const userInput = req.body;

  db.getDB().collection(collection).findOneAndUpdate({_id: 
  db.getPrimaryKey(todoID)}, {$set: {todo: userInput.todo}}, 
 {returnOriginal : false},(err,result)=>{
  if(err)
       console.log(err);
  else{
      res.json(result);
  }
 });
});

app.post('/',(req,res)=>{
  const userInput = req.body;
  db.getDB().collection(collection).insertOne(userInput, 
 (err,result)=>{
    if(err)
       console.log(err);
    else
      res.json({result: result, document: result.ops[0]});
  });
});


app.delete('/:id',(req,res)=>{
   const todoID = req.params.id;

  db.getDB().collection(collection).findOneAndDelete({_id : 
   db.getPrimaryKey(todoID)},(err,result)=>{
    if(err)
      console.log(err);
      else
       res.json(result);
     });
 });

这个在邮递员工作但不在我的应用程序中:

app.get('/', (req,res)=>{
  const todoID = req.params.id;
  db.getDB().collection(collection).findOne({_id: 
  db.getPrimaryKey(todoID)}, (err,documents)=>{
     if(err)
      console.log(err);
     else{
      console.log(documents);
      res.json(documents);
     }
  });
});

这是工作:

  const deleteShipment = (shipment, divShipmentID, deleteID) =>{
    let deleteBtn = $(`#${deleteID}`);
    deleteBtn.click(()=>{
      fetch(`/${shipment._id}`,{
        method: "delete"
      }).then((response)=>{
        return response.json();
      }).then((data)=>{
          if(data.ok == 1){
            $(`#${divShipmentID}`).remove();
          }
      });
    });
  }

这不是:

  const openEditOptions = (shipment, editID) =>{
    let editBtn = $(`#${editID}`);
    editBtn.click(()=>{
      console.log('activated');
      fetch(`/${shipment._id}`,{method: "get"}).then((response)=>{
        return response.json();
      }).then((data)=>{
        console.log(data);
        readyEditForm(shipment);
      });
    });
  }

这是HTML:

<button class="shipment-edit" id="edit_5c6759a05b4290e978136ea0" 
 type="button">Edit</button>

 <button type="button" class="shipment-delete" 
 id="delete_5c6759a05b4290e978136ea0">Delete</button>

我希望特定“装运”的编辑按钮在被点击时记录到控制台'激活',并在控制台和函数readyEditForm中返回装运数据。什么都没发生。

回答如下:

req.params是一种在路径中的:之后获取路径参数的方法。

app.get('/:id' (req, res) => {
console.log(req.params.id); //prints whatever the user put in place of ":id"
});

app.get('/' (req, res) => {
console.log(req.params.id); //prints undefined
});

一旦你修复了代码的这一部分,其余部分应该可以正常工作。

使用html ids查询mongoDB文档

我一直在学习使用mongoDB,Express,JQuery和Node.js构建CRUD应用程序的教程。我现在正在尝试添加自定义功能,当我偏离我模仿的代码时,我无法编辑和检索文档。我很好奇发生了什么。我已经按照删除和编辑功能工作,构建一个新的编辑功能,允许我更新多个字段和不同的用户界面。我还想让文档使用这些字段来填充网站不同部分的各种块的HTML,但是我没有让文档返回。

我确信get和put函数在Postman中有效。

这是什么工作:

app.put('/:id',(req,res) =>{
  const todoID = req.params.id;
  const userInput = req.body;

  db.getDB().collection(collection).findOneAndUpdate({_id: 
  db.getPrimaryKey(todoID)}, {$set: {todo: userInput.todo}}, 
 {returnOriginal : false},(err,result)=>{
  if(err)
       console.log(err);
  else{
      res.json(result);
  }
 });
});

app.post('/',(req,res)=>{
  const userInput = req.body;
  db.getDB().collection(collection).insertOne(userInput, 
 (err,result)=>{
    if(err)
       console.log(err);
    else
      res.json({result: result, document: result.ops[0]});
  });
});


app.delete('/:id',(req,res)=>{
   const todoID = req.params.id;

  db.getDB().collection(collection).findOneAndDelete({_id : 
   db.getPrimaryKey(todoID)},(err,result)=>{
    if(err)
      console.log(err);
      else
       res.json(result);
     });
 });

这个在邮递员工作但不在我的应用程序中:

app.get('/', (req,res)=>{
  const todoID = req.params.id;
  db.getDB().collection(collection).findOne({_id: 
  db.getPrimaryKey(todoID)}, (err,documents)=>{
     if(err)
      console.log(err);
     else{
      console.log(documents);
      res.json(documents);
     }
  });
});

这是工作:

  const deleteShipment = (shipment, divShipmentID, deleteID) =>{
    let deleteBtn = $(`#${deleteID}`);
    deleteBtn.click(()=>{
      fetch(`/${shipment._id}`,{
        method: "delete"
      }).then((response)=>{
        return response.json();
      }).then((data)=>{
          if(data.ok == 1){
            $(`#${divShipmentID}`).remove();
          }
      });
    });
  }

这不是:

  const openEditOptions = (shipment, editID) =>{
    let editBtn = $(`#${editID}`);
    editBtn.click(()=>{
      console.log('activated');
      fetch(`/${shipment._id}`,{method: "get"}).then((response)=>{
        return response.json();
      }).then((data)=>{
        console.log(data);
        readyEditForm(shipment);
      });
    });
  }

这是HTML:

<button class="shipment-edit" id="edit_5c6759a05b4290e978136ea0" 
 type="button">Edit</button>

 <button type="button" class="shipment-delete" 
 id="delete_5c6759a05b4290e978136ea0">Delete</button>

我希望特定“装运”的编辑按钮在被点击时记录到控制台'激活',并在控制台和函数readyEditForm中返回装运数据。什么都没发生。

回答如下:

req.params是一种在路径中的:之后获取路径参数的方法。

app.get('/:id' (req, res) => {
console.log(req.params.id); //prints whatever the user put in place of ":id"
});

app.get('/' (req, res) => {
console.log(req.params.id); //prints undefined
});

一旦你修复了代码的这一部分,其余部分应该可以正常工作。

与本文相关的文章

发布评论

评论列表 (0)

  1. 暂无评论