Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.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/1/dart/3.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
Node.js Post请求在颤振应用程序中发送未定义的数据_Node.js_Dart_Flutter - Fatal编程技术网

Node.js Post请求在颤振应用程序中发送未定义的数据

Node.js Post请求在颤振应用程序中发送未定义的数据,node.js,dart,flutter,Node.js,Dart,Flutter,我正在开发一个具有登录功能的简单应用程序,但我无法将用户名和密码正确发送到nodejs服务器。我尝试过对其进行编码,将其作为Map和FormData,但似乎没有任何效果。我控制台记录了请求主体,并打印“Unfind” 我正在使用Dio dart包进行http请求,并使用Redux和Redux thunk来分派操作。 //我的颤振应用程序上的代码 ThunkAction<AppState> login(LoginData data) { return (St

我正在开发一个具有登录功能的简单应用程序,但我无法将用户名和密码正确发送到nodejs服务器。我尝试过对其进行编码,将其作为Map和FormData,但似乎没有任何效果。我控制台记录了请求主体,并打印“Unfind”

我正在使用Dio dart包进行http请求,并使用Redux和Redux thunk来分派操作。 //我的颤振应用程序上的代码

    ThunkAction<AppState> login(LoginData data) {
          return (Store<AppState> store) async {
            store.dispatch(IsLoading(true));

            try {
              Response response = await Dio().post(
                  "http://10.0.2.2:4000/api/user/login",
                  data: json.encode({"phone": data.phone, "password": data.password}));

              if (response.statusCode == 200) {
                print(json.decode(response.data));
                store.dispatch(IsLoading(false));
              }
            } catch (e) {
              print("Error :(");
            }
          };
        }

// Code on My nodejs 


    router.post("/login", (req, res) => {
    //this log prints undefined
      console.log("Login route: " + req.body.phone);
      var cred = {
        phone: req.body.phone,
        password: req.body.password
      };

      User.findOne({ phone: cred.phone })
        .then(result => {
          if (!result) {
            res.status(400).json({ msg: "no user" });
          } else {
            bcrypt.compare(req.body.password, result.password, (err, isMatch) => {
              if (isMatch) {
                const payload = { id: result._id };
                console.log("Logged in :" + payload);
                jwt.sign(
                  payload,
                  keys.secretOrKey,
                  { expiresIn: 7200 },
                  (err, token) => {
                    res.status(200).json({
                      success: true,
                      token: "Bearer " + token
                    });
                  }
                );
              } else {
                res.status(400).json({ msg: err });
              }
            });
          }
        })
        .catch(err => {
          res.status(400).json({ msg: err });
        });
    });
ThunkAction登录(登录数据){
返回(存储)异步{
存储调度(IsLoading(true));
试一试{
响应=等待Dio().post(
"http://10.0.2.2:4000/api/user/login",
data:json.encode({“phone”:data.phone,“password”:data.password}));
如果(response.statusCode==200){
打印(json.decode(response.data));
存储调度(IsLoading(false));
}
}捕获(e){
打印(“错误:(”);
}
};
}
//我的nodejs上的代码
路由器.post(“/login)”,(请求、回复)=>{
//此日志打印未定义的内容
日志(“登录路径:+req.body.phone”);
变量cred={
电话:req.body.phone,
密码:req.body.password
};
User.findOne({phone:cred.phone})
。然后(结果=>{
如果(!结果){
res.status(400).json({msg:“无用户”});
}否则{
bcrypt.compare(req.body.password,result.password,(err,isMatch)=>{
如果(isMatch){
常量有效负载={id:result.\u id};
console.log(“登录:+有效负载”);
jwt符号(
有效载荷,
钥匙,分泌钥匙,
{expiresIn:7200},
(错误,标记)=>{
res.status(200).json({
成功:没错,
代币:“持票人”+代币
});
}
);
}否则{
res.status(400).json({msg:err});
}
});
}
})
.catch(错误=>{
res.status(400).json({msg:err});
});
});

要访问服务器端的参数,请将此标头添加到您的请求中:

HttpHeaders.contentTypeHeader: 'application/x-www-form-urlencoded'

我也遇到了同样的问题,通过添加
HttpHeaders.contentTypeHeader:“application/x-www-form-urlencoded
头到我的请求中得到了解决。非常感谢它的工作:)@Saman,你可以将此添加为答案而不是评论,因为它解决了海报的问题。