Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.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
Flutter Localhost API在postman上正常工作,但Heroku API不工作| Atlas Mongodb_Flutter_Heroku_Undefined_Mongodb Atlas - Fatal编程技术网

Flutter Localhost API在postman上正常工作,但Heroku API不工作| Atlas Mongodb

Flutter Localhost API在postman上正常工作,但Heroku API不工作| Atlas Mongodb,flutter,heroku,undefined,mongodb-atlas,Flutter,Heroku,Undefined,Mongodb Atlas,我正在创建一个登录表单,我在Atlas上创建了MongoDB,并将我的后端推到Heroku上,一切都很好,我的MongoDB现在已经连接好了,但是当我在postman上调用localhost API时,它工作得很好,当我在postman上调用Heroku API时,它不工作,它打印无效的输入 注意:用户输入无效用户名或密码时打印的输入消息无效 前端代码: 这是我的认证代码 import 'dart:convert'; import 'package:dio/dio.dart'; class

我正在创建一个登录表单,我在Atlas上创建了MongoDB,并将我的后端推到Heroku上,一切都很好,我的MongoDB现在已经连接好了,但是当我在postman上调用localhost API时,它工作得很好,当我在postman上调用Heroku API时,它不工作,它打印无效的输入

注意:用户输入无效用户名或密码时打印的输入消息无效

前端代码:

这是我的认证代码

import 'dart:convert';

import 'package:dio/dio.dart';

class AuthService{
  Dio dio=new Dio();
  login(name,password) async{
  var data = FormData.fromMap({
  'username': name,
  'password': password,
});

// var data={
//   "username":name,
//   "password":password
// };
  try{
  return await dio.post("https://attendance-demo.herokuapp.com/login",
      data: data,
      // options:
          //Options(headers: {"Content-Type": "application/json"}));
         //Options(contentType:Headers.formUrlEncodedContentType))
  );
  }
  catch(e){
   print(e);
  }
  }
  
}
这里我点击按钮调用上面的代码

RoundedButton(text:"Login",  press: () {
                     AuthService().login(name,password).then((value){
                       if(value.data['success']){
                         token=value.data['token'];
                         Fluttertoast.showToast(msg: 'Authenticated',
                         toastLength: Toast.LENGTH_SHORT,
                        gravity: ToastGravity.BOTTOM,
                        backgroundColor: Colors.red,
                        textColor: Colors.white,
                        fontSize: 16.0);
                         
                       }else{
                         print("invalid");
                       }
                     });
                    },
                  ), 
后端代码:

这是我的login.js的后端代码

var body = require('body-parser');
const mongoose = require('mongoose');
var mongodb = require('mongodb')
//var url = 'mongodb://localhost:27017/Mongodb'
var express = require('express');
var app = express();
app.use(body.json());
const User = require('../connection'); 
var bcrypt = require('bcrypt');
var CryptoJS = require("crypto-js")
var jwt = require('jsonwebtoken');

login=(req,res,next)=>{  
  console.log(req.body)
  User.find({"username":req.body.username},function(err,data){
    console.log(data);
    if(err){
      console.log("unspecified name")
      res.status(400).json("unspecified name")
      console.log("not successful")
      return;
    } 
    else{
    console.log(data.length)
         if(data.length<=0)
      {
        res.status(400).json({
          "message":"Invalid Input!"
        })  
      }
      else {
           
        //bcrypt.compare(req.body.password,data[0].password).then(function(result) {
          var bytes = CryptoJS.AES.decrypt(data[0].password, 'my-secret-key@123');
          var decryptedData = JSON.parse(bytes.toString(CryptoJS.enc.Utf8));
          console.log(decryptedData)
            if (req.body.password==decryptedData)
              {
                login = "Succesful";         
                
                var token = jwt.sign({
                  data: 'foobar'
           }, 'secret', { expiresIn: "2 minute"})
           res.status(200).json({auth: true,  AccessToken:token,  User:data[0]}) 
          }
           else
           {res.status(300).json({
            "message":"Input!"
          })}
          
          //  });
          
        }}
      })
}
module.exports = login;

下面是我在postman上调用localhost API时的输出

这是我在postman上调用Heroku API时的输出

下面是Heroku的日志

2021-05-26T13:06:04.123112+00:00 app[web.1]: 0
2021-05-26T13:37:47.537503+00:00 heroku[web.1]: Idling
2021-05-26T13:37:47.539453+00:00 heroku[web.1]: State changed from up to down
2021-05-26T13:37:48.308199+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-05-26T13:37:48.481977+00:00 heroku[web.1]: Process exited with status 143
2021-05-26T21:50:55.760555+00:00 heroku[web.1]: Unidling
2021-05-26T21:50:55.772460+00:00 heroku[web.1]: State changed from down to starting
2021-05-26T21:50:59.537830+00:00 heroku[web.1]: Starting process with command `node app`
2021-05-26T21:51:03.336753+00:00 app[web.1]: Server is running
2021-05-26T21:51:03.802008+00:00 heroku[web.1]: State changed from starting to up
2021-05-26T21:51:04.138933+00:00 app[web.1]: connection Successful
2021-05-26T21:51:04.753704+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=95773277-b522-44f9-aa23-e44e09cd6290 fwd="18.209.1.18" dyno=web.1 connect=1ms service=23ms status=404 bytes=388 protocol=https
2021-05-26T21:51:04.906451+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=9dea2ec9-af60-49ab-b9ac-7df0d2609cdb fwd="34.243.59.93" dyno=web.1 connect=1ms service=19ms status=404 bytes=388 protocol=https
2021-05-26T21:51:05.295475+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=b2a60ca6-ea6b-493a-aaef-ac8ce6f7450d fwd="34.250.3.66" dyno=web.1 connect=0ms service=2ms status=404 bytes=388 protocol=https
2021-05-26T22:28:30.175404+00:00 heroku[web.1]: Idling
2021-05-26T22:28:30.177851+00:00 heroku[web.1]: State changed from up to down
2021-05-26T22:28:31.627550+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-05-26T22:28:31.853029+00:00 heroku[web.1]: Process exited with status 143
2021-05-27T04:17:47.489148+00:00 heroku[web.1]: Unidling
2021-05-27T04:17:47.499418+00:00 heroku[web.1]: State changed from down to starting
2021-05-27T04:17:51.031803+00:00 heroku[web.1]: Starting process with command `node app`
2021-05-27T04:17:54.794963+00:00 app[web.1]: Server is running
2021-05-27T04:17:55.290931+00:00 heroku[web.1]: State changed from starting to up
2021-05-27T04:17:55.732266+00:00 app[web.1]: connection Successful
2021-05-27T04:17:56.646034+00:00 app[web.1]: login api hit
2021-05-27T04:17:56.653451+00:00 app[web.1]: undefined
2021-05-27T04:17:56.743801+00:00 app[web.1]: 0
2021-05-27T04:17:56.753178+00:00 heroku[router]: at=info method=POST path="/login" host=attendance-demo.herokuapp.com request_id=ae3793be-e8cf-41bd-9d1e-88b3f07106b1 fwd="115.42.71.200" dyno=web.1 connect=3ms service=136ms status=400 bytes=244 protocol=https
2021-05-27T04:18:11.974635+00:00 app[web.1]: login api hit
2021-05-27T04:18:11.974852+00:00 app[web.1]: undefined
2021-05-27T04:18:12.047004+00:00 app[web.1]: 0
2021-05-27T04:18:12.050263+00:00 heroku[router]: at=info method=POST path="/login" host=attendance-demo.herokuapp.com request_id=e6231f31-b9f0-4805-83ef-ebd544b63f4e fwd="115.42.71.200" dyno=web.1 connect=1ms service=77ms status=400 bytes=244 protocol=https

请帮忙!非常感谢

2021-05-26T13:06:04.123112+00:00 app[web.1]: 0
2021-05-26T13:37:47.537503+00:00 heroku[web.1]: Idling
2021-05-26T13:37:47.539453+00:00 heroku[web.1]: State changed from up to down
2021-05-26T13:37:48.308199+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-05-26T13:37:48.481977+00:00 heroku[web.1]: Process exited with status 143
2021-05-26T21:50:55.760555+00:00 heroku[web.1]: Unidling
2021-05-26T21:50:55.772460+00:00 heroku[web.1]: State changed from down to starting
2021-05-26T21:50:59.537830+00:00 heroku[web.1]: Starting process with command `node app`
2021-05-26T21:51:03.336753+00:00 app[web.1]: Server is running
2021-05-26T21:51:03.802008+00:00 heroku[web.1]: State changed from starting to up
2021-05-26T21:51:04.138933+00:00 app[web.1]: connection Successful
2021-05-26T21:51:04.753704+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=95773277-b522-44f9-aa23-e44e09cd6290 fwd="18.209.1.18" dyno=web.1 connect=1ms service=23ms status=404 bytes=388 protocol=https
2021-05-26T21:51:04.906451+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=9dea2ec9-af60-49ab-b9ac-7df0d2609cdb fwd="34.243.59.93" dyno=web.1 connect=1ms service=19ms status=404 bytes=388 protocol=https
2021-05-26T21:51:05.295475+00:00 heroku[router]: at=info method=GET path="/login" host=attendance-demo.herokuapp.com request_id=b2a60ca6-ea6b-493a-aaef-ac8ce6f7450d fwd="34.250.3.66" dyno=web.1 connect=0ms service=2ms status=404 bytes=388 protocol=https
2021-05-26T22:28:30.175404+00:00 heroku[web.1]: Idling
2021-05-26T22:28:30.177851+00:00 heroku[web.1]: State changed from up to down
2021-05-26T22:28:31.627550+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2021-05-26T22:28:31.853029+00:00 heroku[web.1]: Process exited with status 143
2021-05-27T04:17:47.489148+00:00 heroku[web.1]: Unidling
2021-05-27T04:17:47.499418+00:00 heroku[web.1]: State changed from down to starting
2021-05-27T04:17:51.031803+00:00 heroku[web.1]: Starting process with command `node app`
2021-05-27T04:17:54.794963+00:00 app[web.1]: Server is running
2021-05-27T04:17:55.290931+00:00 heroku[web.1]: State changed from starting to up
2021-05-27T04:17:55.732266+00:00 app[web.1]: connection Successful
2021-05-27T04:17:56.646034+00:00 app[web.1]: login api hit
2021-05-27T04:17:56.653451+00:00 app[web.1]: undefined
2021-05-27T04:17:56.743801+00:00 app[web.1]: 0
2021-05-27T04:17:56.753178+00:00 heroku[router]: at=info method=POST path="/login" host=attendance-demo.herokuapp.com request_id=ae3793be-e8cf-41bd-9d1e-88b3f07106b1 fwd="115.42.71.200" dyno=web.1 connect=3ms service=136ms status=400 bytes=244 protocol=https
2021-05-27T04:18:11.974635+00:00 app[web.1]: login api hit
2021-05-27T04:18:11.974852+00:00 app[web.1]: undefined
2021-05-27T04:18:12.047004+00:00 app[web.1]: 0
2021-05-27T04:18:12.050263+00:00 heroku[router]: at=info method=POST path="/login" host=attendance-demo.herokuapp.com request_id=e6231f31-b9f0-4805-83ef-ebd544b63f4e fwd="115.42.71.200" dyno=web.1 connect=1ms service=77ms status=400 bytes=244 protocol=https