couchbase : Failed to connect to Couchbase #72148
Answered
by
icyJoseph
VenkataChadalawada
asked this question in
App Router
-
|
I'm trying to connect to my couchbase to do session management and also caching purpose. My code
import getSessionData from './actions/getSessionData';
export const metadata = {
title: "App Router",
};
export default async function Page() {
await getSessionData();
return <h1>App Router</h1>;
}
async function getSessionData() {
try {
const apiUrl = `${process.env.APP_URL}/api/session`;
await fetch(apiUrl, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
});
} catch (error) {
console.error('Failed to get session:', error);
}
}
export default getSessionData;
// pages/api/data.js
// pages/api/data.js
import { getCache } from '../../../server-utils/session-management/couch';
import { NextRequest, NextResponse } from 'next/server';
export async function GET(req: NextRequest) {
const res = new NextResponse();
console.log('/api/session end point triggered');
await getCache('key');
return NextResponse.json({ data: 'success' }, { status: 200 });
}
import couchbase from 'couchbase';
import path from 'path';
const clusterConnStr = process.env.COUCHBASE_URL || ''; // Couchbase URL with SSL
const bucketName = process.env.COUCHBASE_BUCKET || '';
const username = process.env.COUCHBASE_USERNAME || '';
const password = process.env.COUCHBASE_PASSWORD || '';
const collectionName = process.env.COUCHBASE_COLLECTION || '';
const scopeName = process.env.COUCHBASE_SCOPE || '';
export const connectToCouchbase = async () => {
try{
const cluster = await couchbase.connect(clusterConnStr, {
username: username,
password: password,
// Sets a pre-configured profile called "wanDevelopment" to help avoid latency issues
// when accessing Capella from a different Wide Area Network
// or Availability Zone (e.g. your laptop).
configProfile: 'wanDevelopment',
timeouts: {
kvTimeout: 20000, // Increase to 20 seconds or more
queryTimeout: 20000,
connectTimeout: 20000,
},
})
console.log("Connection successful.-----------");
const bucket = cluster.bucket(bucketName);
// console.log("Connection successful.-----------", bucket);
// Get a reference to the default collection, required only for older Couchbase server versions
const defaultCollection = bucket.defaultCollection()
const collection = bucket.scope(scopeName).collection(collectionName)
console.log("Collection selected resulted.-----------");
return collection;
} catch (error) {
console.error("Connection error:", error);
}
};
export const getCache = async (key: string) => {
try {
console.log("key: ", key);
await connectToCouchbase();
console.log("YAY!");
return "success";
} catch (error) {
if (error instanceof couchbase.DocumentNotFoundError) {
return null; // Cache miss
}
throw error; // Handle other errors
}
};Can you please help ? |
Beta Was this translation helpful? Give feedback.
Answered by
icyJoseph
Nov 3, 2024
Replies: 1 comment 1 reply
-
|
Looks like an issue with Couchbase: const nextConfig = {
webpack: (config) => {
config.externals = [...config.externals, 'couchbase'];
return config;
},
}; |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
VenkataChadalawada
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Looks like an issue with Couchbase: