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

节点更新不工作,格式错误,同时发送到API

IT培训 admin 9浏览 0评论

节点更新不工作,格式错误,同时发送到API

我尝试更新我的数据从一定角度的Node.js

component.ts

updatefunction(id,data){
    console.log("component",data);
    //component {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
    this.uAdminService
      .updateUser(id,data).subscribe(
        result => {
          //console.log(result.json());
        },
        error => {
          console.log(error.json());
        }
      );

  }

在myservice.ts

updatefunction(id, data){
      console.log("service", data);
       //service {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
       let headers = new Headers({ 'x-access-token': this.token, 'Content-Type': 'application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        return this.http.put(this.url+id, data, options);
}

我的控制器的NodeJS

router.put('/:id', VerifyToken, function (req, res) {
    console.log(req.body);
    //{'{\n "role": "User", \n "_id": "5c2dc052d6bfba36b41b34dd" \n}'}
        User.findByIdAndUpdate({_id:req.params.id}, req.body, {new: true}).select("-password")
        .then(users => {
            res.send(users);
        }).catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving Report."
            });
        });


});

我req.body控制台这样{ '{\ n “角色”: “用户”,\ n “_id”: “5c2dc052d6bfba36b41b34dd” \ n}'},但我从angularjs通过这种格式{角色: “用户”,_id : “5c2dc052d6bfba36b41b34dd”,名称: “测试”,邮件: “[email protected]”,//用户名: “测试”}

我不知道为什么它转换,这样它不会更新到数据库

如果我从API控制台中会喜欢这个名字{“测试”,邮件:“[email protected]”}

回答如下:

更新myservice.ts象下面这样:

updatefunction(id, data){
      console.log("service", data);
       //service {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
       let headers = new Headers({ 'x-access-token': this.token, 'Content-Type': 'application/json' });
       let options = new RequestOptions({ headers: headers });
       return this.http.put(this.url+id, data, options);
}

改变了Content-Typeapplication/json为您发送json格式化的数据,而不是form数据。

节点更新不工作,格式错误,同时发送到API

我尝试更新我的数据从一定角度的Node.js

component.ts

updatefunction(id,data){
    console.log("component",data);
    //component {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
    this.uAdminService
      .updateUser(id,data).subscribe(
        result => {
          //console.log(result.json());
        },
        error => {
          console.log(error.json());
        }
      );

  }

在myservice.ts

updatefunction(id, data){
      console.log("service", data);
       //service {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
       let headers = new Headers({ 'x-access-token': this.token, 'Content-Type': 'application/x-www-form-urlencoded' });
        let options = new RequestOptions({ headers: headers });
        return this.http.put(this.url+id, data, options);
}

我的控制器的NodeJS

router.put('/:id', VerifyToken, function (req, res) {
    console.log(req.body);
    //{'{\n "role": "User", \n "_id": "5c2dc052d6bfba36b41b34dd" \n}'}
        User.findByIdAndUpdate({_id:req.params.id}, req.body, {new: true}).select("-password")
        .then(users => {
            res.send(users);
        }).catch(err => {
            res.status(500).send({
                message: err.message || "Some error occurred while retrieving Report."
            });
        });


});

我req.body控制台这样{ '{\ n “角色”: “用户”,\ n “_id”: “5c2dc052d6bfba36b41b34dd” \ n}'},但我从angularjs通过这种格式{角色: “用户”,_id : “5c2dc052d6bfba36b41b34dd”,名称: “测试”,邮件: “[email protected]”,//用户名: “测试”}

我不知道为什么它转换,这样它不会更新到数据库

如果我从API控制台中会喜欢这个名字{“测试”,邮件:“[email protected]”}

回答如下:

更新myservice.ts象下面这样:

updatefunction(id, data){
      console.log("service", data);
       //service {role: "User", _id: "5c2dc052d6bfba36b41b34dd", name: "Test", email: "[email protected]", //username: "Test"}
       let headers = new Headers({ 'x-access-token': this.token, 'Content-Type': 'application/json' });
       let options = new RequestOptions({ headers: headers });
       return this.http.put(this.url+id, data, options);
}

改变了Content-Typeapplication/json为您发送json格式化的数据,而不是form数据。

发布评论

评论列表 (0)

  1. 暂无评论