Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/400.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Api调用MEAN4+;_Javascript_Node.js_Angular_Express_Mean Stack - Fatal编程技术网

Javascript Api调用MEAN4+;

Javascript Api调用MEAN4+;,javascript,node.js,angular,express,mean-stack,Javascript,Node.js,Angular,Express,Mean Stack,所以我在一个平均堆栈应用程序中工作,但我的api不正确。。 唯一有效的方法就是得到! 我的post和put似乎不起作用,我想我的语法错了,但我只是在互联网上找不到正确的语法 //GET router.get('/employees', (req, res) => { connection((db) => { db.collection('employees') .find() .toArray(

所以我在一个平均堆栈应用程序中工作,但我的api不正确。。 唯一有效的方法就是得到! 我的post和put似乎不起作用,我想我的语法错了,但我只是在互联网上找不到正确的语法

//GET 
router.get('/employees', (req, res) => {
    connection((db) => {
        db.collection('employees')        
            .find()
            .toArray()
            .then((employees) => {
                response.data = employees;
                res.json(response);
            })
            .catch((err) => {
                sendError(err, res);
            });
    });
});

// POST

router.post('/employees', (req, res) => {

    const employees = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
    db.collection('employees').insert(employees, (err, result) => {
      if (err) { 
        res.send({ 'error': 'An error has occurred' }); 
      } else {
        res.send(result.ops[0]);
      }
    });

  });

//PUT

  router.put('/employees/:id', (req, res) => {
    const id = req.params.id;
    const details = { '_id': new ObjectID(id) };
    const employee = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
    db.collection('employees').update(details, employee, (err, result) => {
      if (err) {
          res.send({'error':'An error has occurred'});
      } else {
          res.send(employee);
      } 
    });
  });

PUT和POST方法没有与已建立的数据库的连接,因此db.collection在这两种方法中都未定义

router.post('/employees', (req, res) => {

            const employees = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
    connection((db) => {
            db.collection('employees').insert(employees, (err, result) => {
              if (err) { 
                res.send({ 'error': 'An error has occurred' }); 
              } else {
                res.send(result.ops[0]);
              }
            });
        });
          });

        //PUT

          router.put('/employees/:id', (req, res) => {
            const id = req.params.id;
            const details = { '_id': new ObjectID(id) };
            const employee = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
            connection((db) => {
            db.collection('employees').update(details, employee, (err, result) => {
              if (err) {
                  res.send({'error':'An error has occurred'});
              } else {
                  res.send(employee);
              } 
            });
    });
          });

PUT和POST方法没有与已建立的数据库的连接,因此db.collection在这两种方法中都未定义

router.post('/employees', (req, res) => {

            const employees = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
    connection((db) => {
            db.collection('employees').insert(employees, (err, result) => {
              if (err) { 
                res.send({ 'error': 'An error has occurred' }); 
              } else {
                res.send(result.ops[0]);
              }
            });
        });
          });

        //PUT

          router.put('/employees/:id', (req, res) => {
            const id = req.params.id;
            const details = { '_id': new ObjectID(id) };
            const employee = { name: req.body.name, age: req.body.age , wage: req.body.wage , place: req.body.place };
            connection((db) => {
            db.collection('employees').update(details, employee, (err, result) => {
              if (err) {
                  res.send({'error':'An error has occurred'});
              } else {
                  res.send(employee);
              } 
            });
    });
          });

你能展示一下你用put和post方法发送的请求的主体吗?当你尝试这两种方法时,你会得到什么样的响应?因此,看起来你没有在post和put中定义db,除非你的代码在文件顶部缺少导入,我们看不到你对请求主体的意思是什么,在《邮递员》中,他说“db未定义”,很可能您需要用连接((db)=>{})将db.collection调用封装在PUT和POST方法中;是的,但是为什么我的Get可以工作呢?你能展示一下你用put和post方法发送的req的主体吗?当你尝试这两种方法时,你会得到什么样的响应?看起来你没有在post和put中定义db,除非你的代码在文件顶部缺少导入,我们看不到你对req主体的意思是什么,在《邮递员》中,他说“db未定义”,很可能您需要用连接((db)=>{})将db.collection调用封装在PUT和POST方法中;是的,确实如此,但为什么我的Get工作?是的,谢谢,我忘记重新启动节点--“谢谢你花时间!!这是固定的:)没问题,很高兴我能帮上忙。请只标记已解决的问题谢谢。是的,谢谢,我忘记重新启动节点--“谢谢你花时间!!这是固定的:)没问题,很高兴我能帮上忙。请把问题标出来,谢谢。