FaunaDB 应用程序 returns 401 但凭据没问题
FaunaDB application returns 401 but credentials are fine
我想试试 FaunaDB,所以我做了一个 NodeJS 应用程序。我遵循了一个教程,该教程制作了一个像推特这样的应用程序。但是,当我尝试访问数据库时,收到 403 未经授权的消息。我已经检查了我的安全密钥,但我仍然遇到同样的错误。任何帮助将不胜感激。
.env 文件:
KEY=randomString
PORT=5000
index.js:
require("dotenv").config();
const app = require("express")();
const faunadb = require("faunadb");
const client = new faunadb.Client({
secret: process.env.KEY,
});
const {
Paginate,
Get,
Select,
Match,
Index,
Create,
Collection,
Lambda,
Var,
Join,
Ref,
} = faunadb.query;
app.listen(5000, () => console.log(`API on http://localhost:${process.env.PORT}`));
app.get("/tweet/:id", async (req, res) => {
try {
const doc = await client.query(
Get(
Ref(
Collection("tweets"),
req.params.id
)
)
)
res.send(doc);
} catch (err) {
res.send(err)
}
});
错误信息:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 853
ETag: W/"355-EPYXYAwyDrJxa8vWUbY5JYPY+pw"
Date: Thu, 22 Jul 2021 11:12:16 GMT
Connection: close
{
"name": "Unauthorized",
"message": "unauthorized",
"description": "Unauthorized",
"requestResult": {
"method": "POST",
"path": "",
"query": null,
"requestRaw": "{\"create\":{\"collection\":\"test\"},\"params\":{\"object\":{\"data\":{\"object\":{\"testField\":\"testValue\"}}}}}",
"requestContent": {
"create": {
"collection": "test"
},
"params": {
"object": {
"data": {
"object": {
"testField": "testValue"
}
}
}
}
},
"responseRaw": "{\"errors\":[{\"code\":\"unauthorized\",\"description\":\"Unauthorized\"}]}",
"responseContent": {
"errors": [
{
"code": "unauthorized",
"description": "Unauthorized"
}
]
},
"statusCode": 401,
"responseHeaders": {
":status": 401,
"www-authenticate": "Basic realm=\"Unauthorized\"",
"x-txn-time": "1626952335964976",
"x-faunadb-build": "070821.200951-e596d0a",
"content-length": "65",
"content-type": "application/json;charset=utf-8"
},
"startTime": 1626952335231,
"endTime": 1626952336270
}
}
像这样实例化客户端:
const client = new faunadb.Client({
secret: process.env.KEY,
});
您正在应用一些默认参数,就像您以这种方式编写代码一样(我只指定了最重要的参数):
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.fauna.com',
scheme: 'https',
});
如果您使用的是美国区域组、欧盟区域组或预览环境,“db.fauna.com”默认域将不适合您。
因此,您需要在构造函数中显式提供域参数。
对于美国地区组:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.us.fauna.com',
scheme: 'https',
});
对于欧盟地区组:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.eu.fauna.com',
scheme: 'https',
});
预览:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.fauna-preview.com',
scheme: 'https',
});
您可以在文档中阅读有关区域组的更多信息:
https://docs.fauna.com/fauna/current/api/fql/region_groups
我想试试 FaunaDB,所以我做了一个 NodeJS 应用程序。我遵循了一个教程,该教程制作了一个像推特这样的应用程序。但是,当我尝试访问数据库时,收到 403 未经授权的消息。我已经检查了我的安全密钥,但我仍然遇到同样的错误。任何帮助将不胜感激。
.env 文件:
KEY=randomString
PORT=5000
index.js:
require("dotenv").config();
const app = require("express")();
const faunadb = require("faunadb");
const client = new faunadb.Client({
secret: process.env.KEY,
});
const {
Paginate,
Get,
Select,
Match,
Index,
Create,
Collection,
Lambda,
Var,
Join,
Ref,
} = faunadb.query;
app.listen(5000, () => console.log(`API on http://localhost:${process.env.PORT}`));
app.get("/tweet/:id", async (req, res) => {
try {
const doc = await client.query(
Get(
Ref(
Collection("tweets"),
req.params.id
)
)
)
res.send(doc);
} catch (err) {
res.send(err)
}
});
错误信息:
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 853
ETag: W/"355-EPYXYAwyDrJxa8vWUbY5JYPY+pw"
Date: Thu, 22 Jul 2021 11:12:16 GMT
Connection: close
{
"name": "Unauthorized",
"message": "unauthorized",
"description": "Unauthorized",
"requestResult": {
"method": "POST",
"path": "",
"query": null,
"requestRaw": "{\"create\":{\"collection\":\"test\"},\"params\":{\"object\":{\"data\":{\"object\":{\"testField\":\"testValue\"}}}}}",
"requestContent": {
"create": {
"collection": "test"
},
"params": {
"object": {
"data": {
"object": {
"testField": "testValue"
}
}
}
}
},
"responseRaw": "{\"errors\":[{\"code\":\"unauthorized\",\"description\":\"Unauthorized\"}]}",
"responseContent": {
"errors": [
{
"code": "unauthorized",
"description": "Unauthorized"
}
]
},
"statusCode": 401,
"responseHeaders": {
":status": 401,
"www-authenticate": "Basic realm=\"Unauthorized\"",
"x-txn-time": "1626952335964976",
"x-faunadb-build": "070821.200951-e596d0a",
"content-length": "65",
"content-type": "application/json;charset=utf-8"
},
"startTime": 1626952335231,
"endTime": 1626952336270
}
}
像这样实例化客户端:
const client = new faunadb.Client({
secret: process.env.KEY,
});
您正在应用一些默认参数,就像您以这种方式编写代码一样(我只指定了最重要的参数):
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.fauna.com',
scheme: 'https',
});
如果您使用的是美国区域组、欧盟区域组或预览环境,“db.fauna.com”默认域将不适合您。
因此,您需要在构造函数中显式提供域参数。
对于美国地区组:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.us.fauna.com',
scheme: 'https',
});
对于欧盟地区组:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.eu.fauna.com',
scheme: 'https',
});
预览:
const client = new faunadb.Client({
secret: process.env.KEY,
domain: 'db.fauna-preview.com',
scheme: 'https',
});
您可以在文档中阅读有关区域组的更多信息: https://docs.fauna.com/fauna/current/api/fql/region_groups