小编Jam*_*esG的帖子

无法将请求主体解析为json:意外字符(\' - \'(代码45))AWS Lambda + API + Postman

我已经尝试了几天来获取从AWS API网关发送到Lambda函数的参数,但我没有成功.

我决定从头开始,所以我跟着他们的演练(http://docs.aws.amazon.com/apigateway/latest/developerguide/getting-started.html#getting-started-new-lambda)

我已经检查了两次这次演练,我已经按照这些步骤进行了调查.

问题

当我从Postman或Swift测试API时,我收到错误:

{"message":"无法将请求体解析为json:意外字符(\' - \'(代码45))的数值:预期数字(0-9)跟随减号,表示有效数值\n at [来源:[B @ c036d15;第1行,第3列]"}

在postman中,当我将结果视为JSON时,我得到了

坏字符串

Lambda函数

该函数是演练中的基本示例:

console.log('Loading event');

exports.handler = function(event, context) {
  var name = (event.name === undefined ? 'No-Name' : event.name);
  console.log('"Hello":"' + name + '"');
  context.done(null, {"Hello":name}); // SUCCESS with message
};
Run Code Online (Sandbox Code Playgroud)

从Lambda控制台测试并使用测试数据时,我得到结果:

{
    "Hello": "TestUser123"
}
Run Code Online (Sandbox Code Playgroud)

从API网关测试中测试时,结果还是:

 {
    "Hello": "TestUser123"
}
Run Code Online (Sandbox Code Playgroud)

任何人都可以看到为什么两个测试控制台都允许这项工作,但是当使用POSTMAN进行测试或在Swift脚本中使用它时它不起作用?

编辑1

在postman中,我已将content-type设置为application/json

该脚本返回默认值:

 {
    "Hello": "user"
}
Run Code Online (Sandbox Code Playgroud)

但是,当我在POSTMAN中添加参数名称TestUser123时,这是它返回错误的时间.

更新1

好的,所以我将映射模板更改为我在另一个答案中找到的模板:

{ "name": "$input.params('name')" }
Run Code Online (Sandbox Code Playgroud)

结果是:

{ …
Run Code Online (Sandbox Code Playgroud)

json amazon-web-services node.js aws-lambda

15
推荐指数
3
解决办法
2万
查看次数

Firebase的云功能:'错误:无法处理请求'

我想把头发拉出来; 这是超级简单的,我有脑冻结或它不是那么简单.

我想要的是

当用户访问时,我试图使用firebase取消缩短缩短的URL:所以
myapp.firebaseappurl.com/url/SHORTENEDLINK
我不会让我添加缩短的URL

我希望输出为:

{
  "url": "https://stackoverflow.com/questions/45420989/sphinx-search-how-to-use-an-empty-before-match-and-after-match"
}
Run Code Online (Sandbox Code Playgroud)

我试过了什么

firebase.json 文件:

{
  "hosting": {
    "public": "public",
    "rewrites": [ {
    "source": "/url/:item",
      "destination": "/url/:item"
    } ]
  }
}
Run Code Online (Sandbox Code Playgroud)

index.js 文件:

const functions = require('firebase-functions');

exports.url = functions.https.onRequest((requested, response) => {

    var uri = requested.url;
    request({
        uri: uri,
        followRedirect: true
      },
      function(err, httpResponse) {
        if (err) {
          return console.error(err);
        }
        response.send(httpResponse.headers.location || uri);
      }
    );

});
Run Code Online (Sandbox Code Playgroud)

结果

当我去的时候,myapp.firebaseappurl.com/url/SHORTENEDLINK我得到以下内容:

Error: could not handle the request
Run Code Online (Sandbox Code Playgroud)

url-shortener node.js firebase firebase-hosting google-cloud-functions

15
推荐指数
2
解决办法
2万
查看次数

Firebase:在URL Rewrite中传递url Param

对于firebase文档中的代码示例,它表示要启动一个url重写,如下所示:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "**",
            "destination": "/index.html"
          } ]
        }
Run Code Online (Sandbox Code Playgroud)

我想将参数传递给索引页面时该怎么办?我试过了:

        "hosting": {
          // Add the "rewrites" section within "hosting"
          "rewrites": [ {
            "source": "/item/**",
            "destination": "/item.html?i=$1"
          } ]
        }
Run Code Online (Sandbox Code Playgroud)

但这没有任何作用..

我也尝试过以下答案:

 "hosting": {
  // Add the "rewrites" section within "hosting"
  "rewrites": [ {
    "source": "/item/:item",
    "destination": "/item.html?i=:item"
  } ]
}
Run Code Online (Sandbox Code Playgroud)

但这只会返回404页面.

url-rewriting firebase firebase-hosting

9
推荐指数
2
解决办法
2701
查看次数

Swift 3:检查Internet(Reocurring)viewDidAppear not

好吧,所以我对Swift很新,我对我要做的事情有点困惑,或者我是否朝着错误的方向前进.(https://github.com/ashleymills/Reachability.swift)

这是我的viewDidLoad方法:

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    checkConnection()

}
Run Code Online (Sandbox Code Playgroud)

然后,我从Reachability GitHub项目中获得了一个带有代码的函数:

func checkConnection() {
    //declare this property where it won't go out of scope relative to your listener
    let reachability = Reachability()!

    reachability.whenReachable = { reachability in
        // this is called on a background thread, but UI updates must
        // be on the main thread, like this:
        DispatchQueue.main.async {
            if reachability.isReachableViaWiFi {
                print("Reachable …
Run Code Online (Sandbox Code Playgroud)

reachability ios swift reachability-swift

8
推荐指数
1
解决办法
803
查看次数

React Native - 如何在另一个函数中使用“useState()”

我正在尝试从另一个函数更新状态的值。

我是这样声明的:


const App () => {

const [positionAX, setPositionAX] = useState(100)
const [positionAY, setPositionAY] = useState(100)

}
Run Code Online (Sandbox Code Playgroud)

除了App我还有另一个功能:

const setPosition = () => {
    setPositionAX(200);
    setPositionAY(200);

}
Run Code Online (Sandbox Code Playgroud)

当我打电话时,setPosition()我收到错误:

找不到变量:setPositionAX

找不到变量:setPositionAY

我试图移到const [positionAX, setPositionAX] = useState(100)文件的顶部,但随后出现错误:

无效的钩子调用。钩子只能在函数体内调用。

如何从其他函数更新该变量?

为@tj-crowder 编辑 1

我似乎无法让它工作,我不确定我做错了什么

这是您给我的代码的现场小吃示例:https : //snack.expo.io/Cd!hwDQ0m

如果我添加警报:

alert("pressed")
Run Code Online (Sandbox Code Playgroud)

进入 handleClick 函数,它起作用了,所以出于某种原因,它只是不能与setPositionA([200, 200]).

另外作为附加说明: 在我的应用程序示例中,有两个功能和设置状态的功能,以及显示状态的功能是分开的,这就是为什么我遇到了我打算在初始堆栈溢出帖子中提出的问题.

这是我试图实现的那个例子的小吃:

https://snack.expo.io/6TUAkpCZM

我使用“positionX”和“positionY”作为变量来定义渲染出的元素的顶部和左侧位置,但是,变量positionXpositionY变量的实际设置是在另一个函数中完成的

我是一名初级开发人员,所以我真的很想感谢你在这方面的所有帮助,如果这是超级菜鸟,我也很抱歉。

编辑 2:穆罕默德·努曼

我试过你的方法,但我似乎无法让它工作。我认为对于我想要实现的目标以及我想要实现的目标存在一些困惑。这可能是我的错,因为我是一个 noobie 开发人员,所以请原谅我。我不想只是发布我所有的代码,因为我认为人们不喜欢仅仅转储代码并像“修复这个”一样 - 但是,对于这种情况,它可能是最好的哈哈。

这是我的代码的完整小吃:https : …

javascript hook scope react-native

8
推荐指数
1
解决办法
1万
查看次数

Swift + Firebase安全无法正常工作

我在Firebase控制台中创建了两个用户,两者都有不同的用户名和电子邮件地址.

我希望他们能够将他们的分数存储在数据库中.这是结构:

AppName
  - GameStats
    - DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
      - Score : 0986
    - Li75C2BYW7bQnKqMmrqLAZ67HUy4
      - Score : 44131
Run Code Online (Sandbox Code Playgroud)

要访问此值并保持同步,我使用此:

let baseRef = FIRDatabase.database().reference(withPath: "GameStats/" + user.uid + "")
let scoreRef = scoreRef.child("Score")

scoreRef.observe(.value, with: { snapshot in
   print(snapshot.value)
})
Run Code Online (Sandbox Code Playgroud)

我想测试这两个用户是否可以访问其他用户的其他信息.我改变了行,以包括其他user.uid如此:

let baseRef = FIRDatabase.database().reference(withPath: "GameStats/Li75C2BYW7bQnKqMmrqLAZ67HUy4")

// Logged in User: DBW9WQEs2sQn9CuPTE9t7Q1qWSz2
Run Code Online (Sandbox Code Playgroud)

由于某种原因,它输出:

Optional(44131)
Run Code Online (Sandbox Code Playgroud)

如果我更改数据库中的值,它会自动将值更新为我放置的值.

这是错误的用户,由于某种原因,它可以访问它.

这些是我的规则:

  {
  "rules": {
    ".read": "auth != null",
    ".write": "auth != null",
      "GameStats": {
         "$user_id": {
         ".write": "auth != null && auth.uid === $user_id && …
Run Code Online (Sandbox Code Playgroud)

ios firebase firebase-security swift firebase-realtime-database

7
推荐指数
1
解决办法
372
查看次数

Swift UIStackView:以编程方式从中心定位元素.

我试图在UIStackView中获取元素,以便从中心平等地定位自己.

这是我想要的效果:

在此输入图像描述

如您所见,我希望两个文本字段彼此间隔相等,并在堆栈视图中对齐中心.

这个堆栈视图将包含我需要排列的1-7个文本字段.

以下是目前的情况:

在此输入图像描述

这就是我设置文本字段的方式

let textLabel = UILabel()
textLabel.backgroundColor = UIColor.red
textLabel.widthAnchor.constraint(equalToConstant: 40.0).isActive = true
textLabel.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
textLabel.text  = "Hi"
textLabel.textAlignment = .center

let textLabel1 = UILabel()
textLabel1.backgroundColor = UIColor.red
textLabel1.widthAnchor.constraint(equalToConstant: 40.0).isActive = true
textLabel1.heightAnchor.constraint(equalToConstant: 20.0).isActive = true
textLabel1.text  = "Hi"
textLabel1.textAlignment = .center
Run Code Online (Sandbox Code Playgroud)

我正在生成堆栈视图,如下所示:

//Stack View
let stackView   = UIStackView()
stackView.axis  = UILayoutConstraintAxis.horizontal
stackView.distribution  = UIStackViewDistribution.equalCentering
stackView.alignment = UIStackViewAlignment.fill
stackView.spacing   = 16.0

stackView.addArrangedSubview(textLabel)
stackView.addArrangedSubview(textLabel1)
stackView.translatesAutoresizingMaskIntoConstraints = false;

self.view.addSubview(stackView)

//Constraints

stackView.leftAnchor.constraint(equalTo: self.view.leftAnchor, constant: 50).isActive = true …
Run Code Online (Sandbox Code Playgroud)

swift uistackview

6
推荐指数
1
解决办法
4560
查看次数

PageViewController-将变量传递给子视图

是)我有的

我有一个ViewController(TutorialViewController)和一个UIPageViewController(TutorialPageViewController)。带有StoryBoard ID的情节提要中还有3个额外的视图:

  • GreenViewController
  • BlueViewController
  • RedViewController

我一直在关注本教程(对作者表示敬意,写得很好)。

在Green View Controller上,我定义了一个变量:

var passedVariable = ""
Run Code Online (Sandbox Code Playgroud)

然后在ViewDidLoad中将其打印出来。

这是两个具有代码的控制器:

UIViewController(TutorialViewController):

class TutorialViewController: UIViewController {

@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var containerView: UIView!


var tutorialPageViewController: TutorialPageViewController? {
    didSet {
        tutorialPageViewController?.tutorialDelegate = self
    }
}


override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let tutorialPageViewController = segue.destinationViewController as? TutorialPageViewController {
        self.tutorialPageViewController = tutorialPageViewController
    }
}

@IBAction func didTapNextButton(sender: UIButton) {
    tutorialPageViewController?.scrollToNextViewController()
}
}

extension TutorialViewController: TutorialPageViewControllerDelegate {

func tutorialPageViewController(tutorialPageViewController: TutorialPageViewController,
    didUpdatePageCount …
Run Code Online (Sandbox Code Playgroud)

ios uipageviewcontroller swift

5
推荐指数
1
解决办法
4480
查看次数

JSON以"\"返回(Lambda)

我正在使用AWS Lambda从开放天气api获取JSON并将其返回.

这是我的代码:

var http = require('http');

exports.handler = function(event, context) {
    var url = "http://api.openweathermap.org/data/2.5/weather?id=2172797&appid=b1b15e88fa797225412429c1c50c122a";
    http.get(url, function(res) {
        // Continuously update stream with data
        var body = '';
        res.on('data', function(d) {
            body += d;
        });
        res.on('end', function() {
            context.succeed(body);
        });
        res.on('error', function(e) {
            context.fail("Got error: " + e.message);
        });
    });
}
Run Code Online (Sandbox Code Playgroud)

它工作并返回JSON,但它在每个之前添加反斜杠"如此:

"{\"coord\":{\"lon\":145.77,\"lat\":-16.92},\"weather\":[{\"id\":803,\"main\":\"Clouds\",\"description\":\"broken clouds\",\"icon\":\"04d\"}],\"base\":\"cmc stations\",\"main\":{\"temp\":303.15,\"pressure\":1008,\"humidity\":74,\"temp_min\":303.15,\"temp_max\":303.15},\"wind\":{\"speed\":3.1,\"deg\":320},\"clouds\":{\"all\":75},\"dt\":1458518400,\"sys\":{\"type\":1,\"id\":8166,\"message\":0.0025,\"country\":\"AU\",\"sunrise\":1458505258,\"sunset\":1458548812},\"id\":2172797,\"name\":\"Cairns\",\"cod\":200}"
Run Code Online (Sandbox Code Playgroud)

这是使用(SwiftJSON)将此检测为有效JSON来停止我的过度服务.

任何人都可以告诉我如何使API信息以正确格式化的JSON出现?

我试过.replace这样:

 res.on('end', function() {

        result = body.replace('\\', '');
        context.succeed(result);
    });
Run Code Online (Sandbox Code Playgroud)

它没有改变任何东西.仍然有相同的输出.

json http-get amazon-web-services aws-lambda openweathermap

5
推荐指数
1
解决办法
4646
查看次数

更改WordPress [图库]短代码的输出网址

我最近安装了ta插件,现在将我的图像从媒体库上传到s3.

我还有FTP的整个上传文件夹到s3,其中包含大约4000张图片.

我已经在我的网站中使用了wordpress gallery短代码然而在某处并以某种方式输出了siteurl.

如何更改此设置以便我可以覆盖我的S3存储桶中的URL?

我承认我不知道我在这里做什么或从哪里开始,我真的很感激你的帮助:)

wordpress gallery

5
推荐指数
1
解决办法
208
查看次数