Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/31.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.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
Angular 从express向mongoDB发送请求_Angular_Mongodb_Express - Fatal编程技术网

Angular 从express向mongoDB发送请求

Angular 从express向mongoDB发送请求,angular,mongodb,express,Angular,Mongodb,Express,我正试图通过Express将信息发布到mongoDB。所有到DB的连接都正常工作,因为get请求工作正常 在我的server.js中,我定义了模式 var todoSchema = new Schema({ taskName: String, createdAt: Date, isDone: Boolean, prioraty: String }, { collection: 'tasks' }); var Model = mongoose.mo

我正试图通过Express将信息发布到mongoDB。所有到DB的连接都正常工作,因为get请求工作正常

在我的server.js中,我定义了模式

var todoSchema = new Schema({
    taskName: String,
    createdAt: Date,
    isDone: Boolean,
    prioraty: String
  }, {
    collection: 'tasks'
  });

var Model = mongoose.model('Model', todoSchema);
然后是app.post请求

app.post('/tasks', function(req, res) {
  var savedata = new Model({
    'taskName': req,
    'isDone': false,
    'createdAt': Date.now(),
    'prioraty': 'medium'
  }).save(function (err, result) {
    if (err) throw err;

    if (result) {
      res.json(result)
    }
  })
});
这根本不起作用

在我的前端,我通过按下按钮来调用它,但什么也没发生

  postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
  }
我怎样才能修好它?如前所述,GET请求工作正常,所以我希望我走的是正确的道路

下面是get请求的例子

app.get('/tasks', (req, res) => {
  Model.find({
   // 'request': query
  }, function(err, result) {
    if (err) throw err;
    if (result) {
      res.json(result)
    } else {
      res.send(JSON.stringify({
        error : 'Error'
      }))
    }
     })
    });

查看您的
POST
taskName
是一个基于模式的字符串,但您正在向它传递完整的
req
对象。如果您在请求中传递的是
req.body
中的属性,那么它应该是
req.body.taskName
中的属性。查看您的
POST
taskName
是一个基于模式的字符串,但您正在传递完整的
req
对象。它应该是
req.body
中的属性类似于
req.body.taskName
,如果这是您在请求中传递的内容。

首先,您需要在前端接收响应

在用:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
如果您使用的是
angularjs
(angular1+)

您需要使用
然后
捕获
来接收
响应

在控制器中:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
如果您使用的是
angular
(angular 2+)

您需要使用
subscribe
作为
this。http
返回
observeable

组件中:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})

第二,正如@andrew提到的

taskName
您应该发送
string
,因为您有
taskName
模式作为
string

var todoSchema = new Schema({
    taskName: String,
})
更新:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
req.body包含请求中提交的数据的键值对 身体。默认情况下,它是未定义的,并且在使用时填充 主体解析中间件,如主体解析器

下面的示例显示如何使用
正文解析中间件
来填充
req.body

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

    app.post('/tasks', function(req, res) {
       console.log(req.body)
    });

首先,您需要在前端接收您的回复

在用:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
如果您使用的是
angularjs
(angular1+)

您需要使用
然后
捕获
来接收
响应

在控制器中:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
如果您使用的是
angular
(angular 2+)

您需要使用
subscribe
作为
this。http
返回
observeable

组件中:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})

第二,正如@andrew提到的

taskName
您应该发送
string
,因为您有
taskName
模式作为
string

var todoSchema = new Schema({
    taskName: String,
})
更新:

postTask(task) {
    return this.http.post('http://localhost:3000/tasks', task);
}
this.service.postTask(task).then(function(response) {
    console.log(response)
}).catch(function(error) {
    console.log(error)
})
this.service.postTask(task).subscribe( response => {
    console.log(response)
})
req.body包含请求中提交的数据的键值对 身体。默认情况下,它是未定义的,并且在使用时填充 主体解析中间件,如主体解析器

下面的示例显示如何使用
正文解析中间件
来填充
req.body

var app = require('express')();
var bodyParser = require('body-parser');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

    app.post('/tasks', function(req, res) {
       console.log(req.body)
    });

您是否
订阅了组件中的
您使用的是哪个版本的
angular
?您应该
订阅
您的
post请求
post
请求添加错误消息,以及您在post请求中发送的
任务
对象的示例,这很有帮助。您是否在组件中订阅
您正在使用的
angular
的哪个版本?您应该
订阅
您的
post请求
post
请求添加错误消息,以及您在post请求中发送的
任务
对象的示例,这会很有帮助。谢谢!这帮了大忙,还有一个问题——我的请求主体现在是空的……那么我该如何解决这个问题呢?”taskName':req,因此,例如,如果我写'taskName':“test3”,它工作正常,但我无法从functionyou console
req.body
,如果
console.log(req),请尝试
req.body.taskName
req.params
您将了解存在的内容您需要使用
bodyParser
获取
req.body
请检查更新的答案。你需要
npm安装body解析器--save
我已经在使用它了。。。如果我尝试控制台req.body,我的console.log会给我空对象{},谢谢!这帮了大忙,还有一个问题——我的请求主体现在是空的……那么我该如何解决这个问题呢?”taskName':req,因此,例如,如果我写'taskName':“test3”,它工作正常,但我无法从functionyou console
req.body
,如果
console.log(req),请尝试
req.body.taskName
req.params
您将了解存在的内容您需要使用
bodyParser
获取
req.body
请检查更新的答案。你需要
npm安装body解析器--save
我已经在使用它了。。。如果我尝试控制台req.body,我的console.log会给我空对象{}