我有以下User模型,并希望确保没有人在数据库中存储空字符串(例如空格)。如果有人输入“”(多个空格)first_name,last_name或者nickname该属性应与值一起保存nil。在 Rails 中,我会通过before_validation回调来解决这个问题。在凤凰城解决这个问题的最佳方法是什么?
defmodule MyApp.User do
use MyApp.Web, :model
schema "users" do
field :first_name, :string
field :last_name, :string
field :nickname, :string
timestamps
end
@required_fields ~w()
@optional_fields ~w(first_name last_name nickname)
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
Run Code Online (Sandbox Code Playgroud) 我目前有 Angular 2 前端和 Phoenix 后端。由于某种原因,我可以很好地执行 GET 请求。在凤凰城我有代码endpoint.ex
plug Corsica, origins: "*"
Run Code Online (Sandbox Code Playgroud)
这允许我执行 GET 请求,但是,当我尝试将 POST 请求发送到同一地址时,我的后端会吐出错误
Invalid preflight CORS request because the header "content-type" is not in :allow_headers
Run Code Online (Sandbox Code Playgroud)
我不确定是否需要从前端发送某些内容,或者是否需要从后端启用某些内容。
在文档中我没有找到如何在 Elixir 中使用捕获组的描述。我怎样才能做到这一点?比如说,我想从字符串中提取一个子字符串并将其替换为其他内容:
~r"\[tag1\](.+?)\[\/tag1\]"
Run Code Online (Sandbox Code Playgroud)
如何访问之间的字符串] [/?
我正在尝试使用 tesla 发出帖子请求,但出现错误:
(CaseClauseError) no case clause matching: %{name: "ecdebit"}
(hackney) /deps/hackney/src/hackney_request.erl:312::hackney_request.handle_body/4
(hackney) /deps/hackney/src/hackney_request.erl:81::hackney_request.perform/2
(hackney) /deps/hackney/src/hackney.erl:372::hackney.send_request/2
(tesla) lib/tesla/adapter/hackney.ex:69: Tesla.Adapter.Hackney.request/5
(tesla) lib/tesla/adapter/hackney.ex:31: Tesla.Adapter.Hackney.call/2
Run Code Online (Sandbox Code Playgroud)
我的请求代码是
request_body = %{
name: "ecdebit",
}
Tesla.post(client, "/contactdb/lists", request_body)
Run Code Online (Sandbox Code Playgroud)
在特斯拉基本网址是:https://api.sendgrid.com/v3并设置授权key。我们如何为 post 请求传递数据?
在特斯拉文档中,将发布请求定义为:
Tesla.post("http://httpbin.org/post", "data", headers: [{"content-type", "application/json"}])
Run Code Online (Sandbox Code Playgroud)
这个星球上有没有人可以帮助摆脱这个小故障:(。
我在前端使用nuxt,API 服务器使用phoenix/elixir,反向代理使用 nginx。
当我尝试制作用于上传文件的表单时,我遇到了麻烦。我如何与csrf?
我使用 Phoenix + ExUnit 进行测试。
我有一些ExUnit.Case文件喜欢DataCase, ConnCase...为我的Models, Controllers....
我第一次尝试阿波罗。我的后端服务器是 Phoenix 框架(长生不老药)。并在http://localhost:4000/api 中运行 所以我尝试在我的代码中使用 apollo 进行第一个查询。
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import ApolloClient from 'apollo-boost';
import { gql } from 'apollo-boost';
const client = new ApolloClient(
{
uri: 'http://localhost:4000/api',
}
);
client.query({
query: gql`
{
users {
name
email
}
`}
}).then(result => console.log(result));
Run Code Online (Sandbox Code Playgroud)
但我有一个错误。
[Network error]: TypeError: Network request failed
node_modules/expo/build/logs/LogSerialization.js:166:14 in _captureConsoleStackTrace
node_modules/expo/build/logs/LogSerialization.js:41:24 in serializeLogDataAsync
... 9 more stack frames from framework internals
[Unhandled promise rejection: Error: Network error: …Run Code Online (Sandbox Code Playgroud) 某个数字从控制器传递到模板。例如,这个数字是 5。在模板中,我需要显示从 1 到给定数字 (5) 的数字。例如,在 PHP 中,这可以像这样完成:
for($i=1; $i<=given_number; $i++){
echo $i;
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法弄清楚在 Elixir/Phoenix 中哪个是最好的方法。我会很感激你的帮助。
我正在努力验证 Paypal webhook 数据,但我遇到了一个问题,它总是为验证状态返回 FAILURE。我想知道是不是因为这一切都发生在沙盒环境中,而 Paypal 不允许对沙盒 webhook 事件进行验证?我按照这个 API 文档来实现调用:https : //developer.paypal.com/docs/api/webhooks/v1/#verify-webhook-signature
相关代码(来自单独的 elixir 模块):
def call(conn, _opts) do
conn
|> extract_webhook_signature(conn.params)
|> webhook_signature_valid?()
|> # handle the result
end
defp extract_webhook_signature(conn, params) do
%{
auth_algo: get_req_header(conn, "paypal-auth-algo") |> Enum.at(0, ""),
cert_url: get_req_header(conn, "paypal-cert-url") |> Enum.at(0, ""),
transmission_id: get_req_header(conn, "paypal-transmission-id") |> Enum.at(0, ""),
transmission_sig: get_req_header(conn, "paypal-transmission-sig") |> Enum.at(0, ""),
transmission_time: get_req_header(conn, "paypal-transmission-time") |> Enum.at(0, ""),
webhook_id: get_webhook_id(),
webhook_event: params
}
end
def webhook_signature_valid?(signature) do
body = Jason.encode!(signature)
case …Run Code Online (Sandbox Code Playgroud) 我们来描述一下问题:
mix phx.new{dev, test}.exs(我正在映射一个现有的数据库)mix phx.gen.context(它创建了一个迁移)的上下文我第一次尝试运行服务器,但它告诉我我有未部署的迁移。
there are pending migrations for repo: Some.Repo.
Try running `mix ecto.migrate` in the command line to migrate it
Run Code Online (Sandbox Code Playgroud)
然后我意识到我不需要它们,因为我已经有了数据库,所以我删除了迁移文件 ( /priv/repo/migrations/*) 并再次尝试。
现在mix ecto.migrations什么都不显示,但它没有删除服务器提示。然后我发现 ecto 在数据库中为迁移创建了一个额外的表,所以我检查了它,它是空的。
我放弃了它并尝试再次运行服务器,但显示了相同的消息。
为了确保这不是 Ecto 问题,我准备了测试并且它们运行得很好,唯一的问题是运行服务器时显示的迁移提示。
我还没有任何端点,因为我计划在验证模型正常工作后使用 GraphQL,但该消息令人困惑。
该迁移是否有任何隐藏文件,或者我是否遗漏了其他内容?
堆栈跟踪:
[error] #PID<0.451.0> running Some.Endpoint (connection #PID<0.449.0>, stream id 1) terminated
Server: localhost:4000 (http)
Request: GET /
** (exit) an exception was raised:
** (Phoenix.Ecto.PendingMigrationError) there are pending migrations for repo: …Run Code Online (Sandbox Code Playgroud)