Hey everyone, to preface I’m getting an unauthorized bug that I’ve seen in a few other posts in this forum, but the solutions posted in those posts haven’t helped fix the issue.
This is all the code that’s running to access my FaunaDB which is created with demo data and the Region Group is United States.
import faunadb from 'faunadb';
/* configure faunaDB Client with our secret */
const q = faunadb.query;
const client = new faunadb.Client({
secret: process.env.FAUNADB_SECRET,
domain: 'db.us.fauna.com',
scheme: 'https',
});
exports.handler = (event, context, callback) => {
// return callback(null, {
// statusCode: 200,
// body: JSON.stringify('testing testing'),
// });
return client
.query(q.Get(q.Ref(q.Collection('customers'), '101')))
.then((response) => {
console.log('success', response);
return callback(null, {
statusCode: 200,
body: JSON.stringify(response),
});
})
.catch((error) => {
console.log('error', error);
return callback(null, {
statusCode: 400,
body: JSON.stringify(error),
});
});
};
The resulting error message I get is:
Request from 127.0.0.1: POST /.netlify/functions/todos-create
error [Unauthorized: unauthorized] {
description: 'Unauthorized',
requestResult: r {
method: 'POST',
path: '',
query: null,
requestRaw: '{"get":{"ref":{"collection":"customers"},"id":"101"}}',
requestContent: u { raw: [Object] },
responseRaw: '{"errors":[{"code":"unauthorized","description":"Unauthorized"}]}',
responseContent: { errors: [Array] },
statusCode: 401,
responseHeaders: [Object: null prototype] {
':status': 401,
'www-authenticate': 'Basic realm="Unauthorized"',
'x-txn-time': '1636689123292666',
'x-faunadb-build': '211110.020048-b14d8b0',
'content-length': '65',
'content-type': 'application/json;charset=utf-8',
[Symbol(nodejs.http2.sensitiveHeaders)]: []
},
startTime: 1636689123004,
endTime: 1636689123274
}
}
Response with status 400 in 308 ms.
/Users/arshaan/Desktop/development/crop-farming/node_modules/netlify-lambda/lib/serve.js:35
response.statusCode = lambdaResponse.statusCode;
Note: In the security tab I created a new key for the database and it has admin permissions, it is saved as in .env.development.local
.
FAUNADB_SECRET=XXX123XXX