{"id":10367,"date":"2023-07-18T11:10:47","date_gmt":"2023-07-18T18:10:47","guid":{"rendered":"https:\/\/www.couchbase.com\/developers\/tutorials\/quickstart-nextjs\/"},"modified":"2023-07-18T11:10:47","modified_gmt":"2023-07-18T18:10:47","slug":"quickstart-nextjs","status":"publish","type":"tutorials","link":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/","title":{"rendered":"Using Next.js with Couchbase NodeJS SDK"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/gitpod.io\/#https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\"><img decoding=\"async\" src=\"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal\" alt=\"Try it now!\"><\/a><\/p>\n\n\n\n<h3 id=\"h-prerequisites\" class=\"wp-block-heading\">Prerequisites<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To run this prebuilt project, you will need:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Couchbase Capella cluster or Couchbase 7 installed locally<\/li>\n\n\n<li>NodeJS &amp; NPM (v12+)<\/li>\n\n\n<li>NextJS<\/li>\n\n\n<li>Code Editor<\/li>\n\n\n<li>Follow the <a href=\"\/tutorial-couchbase-installation-options\">Couchbase Installation Options tutorial<\/a> for more info on installing Couchbase<\/li>\n\n<\/ul>\n\n\n\n<h3 id=\"h-what-we-ll-cover\" class=\"wp-block-heading\">What We&#8217;ll Cover<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Bootstrapping a new Next.js app using the &#8216;with-couchbase&#8217; example, building out a simple REST API using the API framework built into Next.js and the <a href=\"https:\/\/docs.couchbase.com\/nodejs-sdk\/current\/hello-world\/start-using-sdk.html\">NodeJS SDK for Couchbase<\/a> version 3.x with the following endpoints:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"#post-a-profile\">Post a Profile<\/a> \u2013 Create a new user profile<\/li>\n\n\n<li><a href=\"#get-a--profile-by-key\">GET a Profile by Key<\/a> \u2013 Get a specific profile<\/li>\n\n\n<li><a href=\"#put-profile\">PUT Profile<\/a> \u2013 Update a profile<\/li>\n\n\n<li><a href=\"#delete-profile\">DELETE Profile<\/a> \u2013 Delete a profile<\/li>\n\n\n<li><a href=\"#get-profiles-by-searching\">GET Profiles by Searching<\/a>  \u2013 Get all profiles matching First or Last Name<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also build out a basic front-end application with Next.js to interact with the endpoints mentioned above.<\/p>\n\n\n\n<h3 id=\"h-source-code\" class=\"wp-block-heading\">Source Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The completed source code from this tutorial can be cloned with the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>git clone https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\n<\/code><\/pre>\n\n\n\n<h4 id=\"h-update-environment-variables-appropriately\" class=\"wp-block-heading\">Update environment variables appropriately<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ve included a <code>.env.local.example<\/code> file with blank values for you to copy into a file called <code>.env.local<\/code> and fill in the values. We&#8217;ve also included a <code>.env.default<\/code> file for testing and running in GitPod. In most cases, you can ignore the default config file.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>CB_USERNAME<\/code> &#8211; The username of an authorized user on your cluster. Follow <a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/manage-database-users.html#create-database-credentials\">these instructions<\/a> to create database credentials on Capella<\/li>\n\n\n<li><code>CB_PASSWORD<\/code> &#8211; The password that corresponds to the user specified above<\/li>\n\n\n<li><code>CB_CONNECT_STRING<\/code> &#8211; The Couchbase endpoint to connect to. Use <code>couchbase:\/\/localhost<\/code> for a local\/Docker cluster, or the connection string for a Capella database (formatted like <code>couchbase:\/\/cb.&lt;xxxxxx&gt;.cloud.couchbase.com<\/code>)<\/li>\n\n\n<li><code>CB_BUCKET<\/code> &#8211; The bucket you&#8217;d like to connect to. Set this to <code>user_profiles<\/code> for this tutorial.<\/li>\n\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>NOTE on TLS:<\/strong> The connection logic in this sample app ignores mismatched certificates with the parameter <code>tls_verify=none<\/code>. While this is helpful in streamlining the connection process for development purposes, it should <strong>NOT<\/strong> be used in production. To learn how to better secure your connection with proper certificates, see <a href=\"https:\/\/developer.couchbase.com\/tutorial-nodejs-tls-connection\">the Node.js TLS connection tutorial<\/a>.<\/p>\n\n\n\n<h3 id=\"h-set-up-and-run-the-application\" class=\"wp-block-heading\">Set up and Run The Application<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll start by bootstrapping a new Next.js project using the &#8216;with-couchbase&#8217; example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>npx create-next-app --example with-couchbase with-couchbase-app\n\n# or\n\nyarn create next-app --example with-couchbase with-couchbase-app\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Install required dependencies and run:<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>npm install\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>If you are using Capella<\/strong>, you&#8217;ll have to manually create a bucket named <code>user_profile<\/code> and a collection named <code>profile<\/code>. See the documentation on <a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/data-service\/manage-buckets.html\">managing buckets<\/a> and <a href=\"https:\/\/docs.couchbase.com\/cloud\/clusters\/data-service\/scopes-collections.html#create-a-collection\">creating a collection<\/a> for more information. Note that this collection should be created on the <code>_default<\/code> scope.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have Couchbase running locally, we can the bucket and collection by running the following command:<\/p>\n\n\n\n<pre class=\"wp-block-code language-shell\"><code>npm run init-db:local\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Extra Step for Capella Clusters<\/strong>: if you&#8217;ve manually set up your bucket and collection, you&#8217;ll need to create the necessary indices as well. To accomplish this, run:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>npm run build-indexes\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This is because the index creation code is contained within the database initialization script, which we don&#8217;t use for Capella clusters. Learn more in the section on <a href=\"#creating-primary-indexes\">Creating Primary Indexes<\/a> section, and take a look at the example code in the <code>util\/initializeCbServer.js<\/code> file to learn how you can programmatically create buckets, collections, and indexes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now we&#8217;re ready to run our application:<\/p>\n\n\n\n<pre class=\"wp-block-code language-sh\"><code>npm run dev\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-document-structure\" class=\"wp-block-heading\">Document Structure<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We will be setting up a REST API to manage some profile documents. Our profile document will have an auto-generated UUID for its key, first and last name of the user, and an email. For this demo we will store individual profiles in documents that belong to collection named <code>profile<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-json\"><code>{\n  &quot;pid&quot;: &quot;b181551f-071a-4539-96a5-8a3fe8717faf&quot;,\n  &quot;firstName&quot;: &quot;John&quot;,\n  &quot;lastName&quot;: &quot;Wick&quot;,\n  &quot;email&quot;: &quot;johnwick@couchbase.com&quot;\n}\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-let-s-review-the-starter-code\" class=\"wp-block-heading\">Let&#8217;s Review the Starter Code<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The starter application has two main files, <code>util\/couchbase.js<\/code> and <code>pages\/index.js<\/code>. We&#8217;ll add a few files to fill out our application further, but first let&#8217;s review each of these.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>util\/couchbase.js<\/code>:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>couchbase.js<\/code> file contains all logic for connecting to the database. It validates environment variables and checks for an open connection before creating and returning cluster, bucket, and collection objects. Our other files can import the <code>connectToDatabase()<\/code> function to gain access to each of these objects.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export async function connectToDatabase() {\n  const cluster = await createCouchbaseCluster()\n  const bucket = cluster.bucket(CB_BUCKET);\n  const collection = bucket.defaultCollection();\n  const profileCollection = bucket.collection(&#039;profile&#039;);\n\n  let dbConnection = {\n    cluster,\n    bucket,\n    collection,\n    profileCollection,\n  }\n\n  return dbConnection;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note the addition of <code>profileCollection<\/code> to this function, which specifies which collection we&#8217;ll use for this project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>pages\/index.js<\/code>:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The index page renders content for our homepage and includes a <code>getServerSideProps()<\/code> function that is automatically run on each page load and injects props into the React component rendered by the index page.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export async function getServerSideProps(context) {\n  let connection = await connectToDatabase();\n  const {cluster, bucket, collection, profileCollection} = connection;\n  \/\/ checks connection\n  return {\n    props: { isConnected },\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The logic for checking the connection can be viewed in the file and is included in the <code>with-couchbase<\/code> Next.js example. If your connection is working, you are ready to move onto the next steps and start coding!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><code>pages\/_app.js<\/code><\/strong>\nThis file just provides a wrapper App component to provide more control on page initialization. This allows us to use the global stylesheet. See <a href=\"https:\/\/nextjs.org\/docs\/advanced-features\/custom-app%5Bthis\">https:\/\/nextjs.org\/docs\/advanced-features\/custom-app[this<\/a> article] from the Next.js docs for more info.<\/p>\n\n\n\n<h3 id=\"h-creating-primary-indexes\" class=\"wp-block-heading\">Creating Primary Indexes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">In order to ensure queries run, we need to create two primary indexes, one for our user_profile bucket and another for our <code>profile<\/code> collection. The collection index is used by the <code>\"\/api\/user\"<\/code> GET endpoint that utilizes a N1QL query to search the database for profile documents where <code>firstName<\/code> or <code>lastName<\/code> match the search value. The bucket index can be used in the case that any documents are added to the bucket&#8217;s default collection or manually from the Couchbase Web UI.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are several ways to build these indexes. For example, you could run the following <code>CREATE PRIMARY INDEX ...<\/code> lines as queries in the query workbench on the web UI. However, the following function accomplishes index creation and can be called from <code>util\/initializeCbServer.js<\/code> so that indexes will be automatically created along with the buckets and collections (using <code>npm run init-db:local<\/code>).<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export const ensureIndexes = async(CB_BUCKET) =&gt; {\n  let {cluster} = await connectToDatabase();\n  try {\n    const bucketIndex = `CREATE PRIMARY INDEX ON ${CB_BUCKET}`\n    const collectionIndex = `CREATE PRIMARY INDEX ON default:${CB_BUCKET}._default.profile;`\n    await cluster.query(bucketIndex)\n    await cluster.query(collectionIndex)\n    console.log(`Index Creation: SUCCESS`)\n  } catch (err) {\n    if (err instanceof couchbase.IndexExistsError) {\n      console.info(&#039;Index Creation: Indexes Already Exists&#039;)\n    } else {\n      console.error(err)\n    }\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we can move onto reviewing each of the individual endpoints. Next.js has a built-in API framework that we&#8217;ll leverage to interact with our Couchbase data source.<\/p>\n\n\n\n<h3 id=\"h-api-setup\" class=\"wp-block-heading\">API Setup<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll start by adding an <code>api\/<\/code> directory inside the <code>pages\/<\/code> directory. Any file within this directory will be mapped to an API endpoint, so we&#8217;ll also create a file called <code>user.js<\/code> and add a handler function. This function will handle ALL request types to this endpoint, so we&#8217;ll also need to check the request type and handle it accordingly. We&#8217;ll also need to connect to the database and parse the body if it exists.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export default async function handler(req, res) {\n  const {cluster, bucket, profileCollection} = await connectToDatabase();\n  let body = !!req.body ? JSON.parse(req.body) : null;\n  if (req.method === &#039;POST&#039;) {\n    \/\/ handle POST request\n  } else if (req.method === &#039;PUT&#039;) {\n    \/\/ handle PUT request\n  } else if (req.method === &#039;GET&#039;) {\n    \/\/ handle GET request (search)\n  } else if (req.method === &#039;DELETE&#039;) {\n    \/\/ handle DELETE request\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we&#8217;ll fill in logic to handle each of the request types.<\/p>\n\n\n\n<h3 id=\"h-post-a-profile\" class=\"wp-block-heading\">POST a Profile<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After checking that the body contains the proper fields, we create a profile document using the SDK <code>.insert()<\/code> method using the <code>profileCollection<\/code>. Note that you will need to install the <code>uuid<\/code> package (to generate unique IDs) via npm or yarn and import them in <code>user.js<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>if (req.method === &#039;POST&#039;) {\n  \/**\n   *  POST HANDLER\n   *\/\n  if (!body.email) {\n    return res.status(400).send({\n      &quot;message&quot;: &#039;email is required&#039;\n    })\n  }\n\n  const id = v4();\n  const profile = {\n    pid: id,\n    ...body,\n  }\n  await profileCollection.insert(profile.pid, profile)\n      .then((result) =&gt; {\n        res.status(201).send({...profile, ...result});\n      })\n      .catch((e) =&gt; {\n        res.status(500).send({\n          &quot;message&quot;: `Profile Insert Failed: ${e.message}`\n        })\n      })\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s break this code down.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">First, we check that an email was sent with the body and then create a <code>profile<\/code> object based on the data that was sent in the request. The <code>pid<\/code> that we\u2019re saving into the account object is a unique key.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After we check for required body parameters, we can asynchronously write to the <code>profileCollection<\/code> using the <code>insert<\/code> method and then return the document saved back to the user. We utilize the spread operator again to make this simple. <code>insert<\/code> is a basic key-value operation.<\/p>\n\n\n\n<h4 id=\"h-create-profile-ui\" class=\"wp-block-heading\">Create Profile UI<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE: The UI source code for this quickstart guide is quite a bit more complex (due to styling and layout) than the inline code blocks outlined in this tutorial. The functionality, however, is the same. The original code can be found in the <a href=\"https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\/tree\/v1\">v1 branch<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Next, lets add a simple front-end form to create user profiles. On the <code>index.js<\/code> page, remove all markup between the &lt;main&gt; tags and add the following form:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form onSubmit={handleProfilePost}&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;First Name&quot; name=&quot;firstName&quot;\/&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;Last Name&quot; name=&quot;lastName&quot;\/&gt;\n    &lt;input type=&quot;email&quot; placeholder=&quot;Email&quot; name=&quot;email&quot;\/&gt;\n    &lt;button type=&quot;submit&quot;&gt;Post Profile&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll handle this request in a function on <code>index.js<\/code> inside <code>Home()<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const handleProfilePost = async (event) =&gt; {\n    await fetch(&quot;http:\/\/localhost:3000\/api\/user&quot;, {\n        method: &#039;POST&#039;,\n        body: JSON.stringify({\n            firstName: event.target.firstName.value,\n            lastName: event.target.lastName.value,\n            email: event.target.email.value,\n        })\n    })\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note: in the completed quickstart code, fetch URLs use a dynamic <code>origin<\/code> variable instead of hard coding <code>http:\/\/localhost:3000<\/code> to ensure requests work when running in other environments.<\/p>\n\n\n\n<h3 id=\"h-get-a-profile-by-key\" class=\"wp-block-heading\">GET a Profile by Key<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Although the API framework baked into Next.js is highly powerful, Next is ALSO capable of fetching data from the database using the <code>getServerSideProps()<\/code> function in <code>index.js<\/code>. We&#8217;ll fetch a profile by key using this method to demonstrate the versatility of Next.js.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Add the following function to the bototm of <code>index.js<\/code> to retrieve a Profile by Profile ID using the SDK <code>.get()<\/code> method<code>using the<\/code>profileCollection`.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>async function getProfileByKey(collection, key) {\n  try {\n    let res = await collection.get(key);\n    return res.content;\n  } catch (err) {\n    return err;\n  }\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We only need the profile ID from the user to retrieve a particular profile document using a basic key-value operation. We can catch the error if the key-value operation fails and return an error message.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can now call this function from <code>getServerSideProps()<\/code> and simply inject the return value into the props:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>  let profile = JSON.parse(JSON.stringify(await getProfileByKey(collection, &#039;&lt;ADD AN EXISTING PID HERE&gt;&#039;)));\n\n  return {\n    props: {isConnected, profile },\n  }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that we&#8217;ll stringify then re-parse the data to avoid any issues with JSON serialization. This is just a quirk of Next.js that can sometimes cause a bug, so it&#8217;s better to ensure proper serialization.<\/p>\n\n\n\n<h4 id=\"h-read-profile-ui\" class=\"wp-block-heading\">Read Profile UI<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE: The UI source code for this quickstart guide is quite a bit more complex (due to styling and layout) than the inline code blocks outlined in this tutorial. The demo app uses a <code>UserRow<\/code> component rather than a <code>UserCard<\/code>, but the functionality of each is roughly the same. The original code can be found in the <a href=\"https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\/tree\/v1\">v1 branch<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">To display the user we&#8217;ve fetched, we&#8217;ll also add a custom React component. To accomplish this, create a new directory outside of <code>pages\/<\/code> and call it <code>components<\/code>. Within <code>components\/<\/code> add a <code>UserCard.js<\/code> file for the following component:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export const UserCard = (props) =&gt; {\n  return (\n      &lt;div style={{marginRight: &#039;10px&#039;, marginLeft: &#039;10px&#039;, border: &#039;1px solid #8f8f8f&#039;, borderRadius: &#039;10px&#039;, padding: &#039;10px&#039;}}&gt;\n        &lt;p&gt;&lt;strong&gt;{props.firstName}&lt;\/strong&gt; {props.lastName}&lt;\/p&gt;\n        &lt;p&gt;{props.email}&lt;\/p&gt;\n        &lt;em&gt;{!!props.pid &amp;&amp; &#039;PID: &#039; + props.pid}&lt;\/em&gt;\n      &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now we can go back to <code>index.js<\/code> and add the following markup to display the user we&#8217;ve fetched:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;UserCard firstName={profile.firstName} lastName={profile.lastName} email={profile.email} pid={profile.pid} \/&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once we can see the profile, lets add logic to edit it.<\/p>\n\n\n\n<h3 id=\"h-put-profile\" class=\"wp-block-heading\">PUT Profile<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Update a Profile by Profile ID by using the SDK <code>.upsert()<\/code> method on the <code>profileCollection<\/code>. We&#8217;ll add the following code to our <code>user.js<\/code> file to handle PUT requests:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>  try {\n      await profileCollection.get(req.query.pid)\n          .then(async (result) =&gt; {\n            \/* Create a New Document with new values,\n              if they are not passed from request, use existing values *\/\n            const newDoc = {\n              pid: result.content.pid,\n              firstName: body.firstName ? body.firstName : result.content.firstName,\n              lastName: body.lastName ? body.lastName : result.content.lastName,\n              email: body.email ? body.email : result.content.email\n            }\n\n            \/* Persist updates with new doc *\/\n            await profileCollection.upsert(req.query.pid, newDoc)\n                .then((result) =&gt; res.send({ ...newDoc, ...result }))\n                .catch((e) =&gt; res.status(500).send(e))\n          })\n          .catch((e) =&gt; res.status(500).send({\n            &quot;message&quot;: `Profile Not Found, cannot update: ${e.message}`\n          }))\n    } catch (e) {\n      console.error(e)\n    }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We don&#8217;t need to specify the <code>pid<\/code> as it already exists, so when we create the profile document, we just need the profile information (<code>firstName<\/code>, <code>lastName<\/code>, and <code>email<\/code>). The user may only be changing one or many fields in the document so we first retrieve the existing document and check for differences and only update the fields needed to be changed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We first look up the existing document and make sure it exists, if it does not, return a 500 level error code and message: &#8220;Cannot update: document not found&#8221;.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, all changed fields in the document get replaced except for the document key and the <code>pid<\/code> field.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we replace the existing fields if we have a value from the HTTP Request (<code>req.body.whatever<\/code>). If we do not have a value in the request for a specific field, we simply reuse the existing document&#8217;s `result.value.whatever.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we create an async call to the <code>profileCollection<\/code> using the <code>upsert<\/code> method and then return the document saved and the result just as we did in the previous endpoint.<\/p>\n\n\n\n<h4 id=\"h-edit-profile-ui\" class=\"wp-block-heading\">Edit Profile UI<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE: The UI source code for this quickstart guide is quite a bit more complex (due to styling and layout) than the inline code blocks outlined in this tutorial. The functionality, however, is the same. The original code can be found in the <a href=\"https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\/tree\/v1\">v1 branch<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s add another front-end form to enable editing:<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form onSubmit={handleProfilePut}&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;PID to Update&quot; name=&quot;pid&quot;\/&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;New First Name&quot; name=&quot;firstName&quot;\/&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;New Last Name&quot; name=&quot;lastName&quot;\/&gt;\n    &lt;input type=&quot;email&quot; placeholder=&quot;New Email&quot; name=&quot;email&quot;\/&gt;\n    &lt;button type=&quot;submit&quot;&gt;Update Profile&lt;\/button&gt;\n&lt;\/form&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can handle edits with the following function:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const handleProfilePut = async (event) =&gt; {\n  await fetch(`http:\/\/localhost:3000\/api\/user?pid=${event.target.pid.value}`, {\n    method: &#039;PUT&#039;,\n    body: JSON.stringify({\n      firstName: event.target.firstName.value,\n      lastName: event.target.lastName.value,\n      email: event.target.email.value\n    })\n  })\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">By pasting the PID into the field and filling in any of the form fields in the edit form, you&#8217;ll be able to edit the document of the PID specified.<\/p>\n\n\n\n<h3 id=\"h-delete-profile\" class=\"wp-block-heading\">DELETE Profile<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To delete profiles, we&#8217;ll first need to update our <code>UserCard<\/code> with a delete button:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>export const UserCard = (props) =&gt; {\n  const handleDeletion = async (event) =&gt; {\n    await fetch(`http:\/\/localhost:3000\/api\/user?pid=${props.pid}`, {\n      method: &#039;DELETE&#039;,\n    }).then(async (data) =&gt; {\n      console.log(data);\n    })\n  }\n  return (\n      &lt;div style={{marginRight: &#039;10px&#039;, marginLeft: &#039;10px&#039;, border: &#039;1px solid #8f8f8f&#039;, borderRadius: &#039;10px&#039;, padding: &#039;10px&#039;}}&gt;\n        &lt;p&gt;&lt;strong&gt;{props.firstName}&lt;\/strong&gt; {props.lastName}&lt;\/p&gt;\n        &lt;p&gt;{props.email}&lt;\/p&gt;\n        &lt;em&gt;{!!props.pid &amp;&amp; &#039;PID: &#039; + props.pid}&lt;\/em&gt;\n        &lt;br\/&gt;\n        &lt;button onClick={handleDeletion}&gt;Delete&lt;\/button&gt;\n      &lt;\/div&gt;\n  );\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Next, we&#8217;ll add the following to <code>user.js<\/code> to handle DELETE requests. Delete Profile by Profile ID by using the SDK <code>.delete()<\/code> method on the <code>profileCollection<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>try {\n      await profileCollection.remove(req.query.pid)\n          .then((result) =&gt; {\n            res.status(200).send(&quot;Successfully Deleted: &quot; + req.query.pid)\n          })\n          .catch((error) =&gt; res.status(500).send({\n            &quot;message&quot;: `Profile Not Found, cannot delete: ${error.message}`\n          }))\n    } catch (e) {\n      console.error(e)\n    }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We only need the profile ID from the user to delete using a basic key-value operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you&#8217;ll see a &#8216;Delete&#8217; button on the user profile we&#8217;ve fetched via a hard-coded id in <code>getServerSideProps()<\/code>. To avoid any errors that may stem from the deletion of this hardcoded document we&#8217;re fetching, let&#8217;s just comment that logic out. We&#8217;re going to add a more robust &#8216;GET&#8217; route in the next step.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>  \/\/ let profile = JSON.parse(JSON.stringify(await getProfileByKey(profileCollection, &#039;1cfaaa82-e63e-4207-addf-f023763d0374&#039;)));\n\n  return {\n    props: {isConnected, \/* profile *\/ },\n  }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Note that you must also remove\/comment out the UserCard markup for this profile.<\/p>\n\n\n\n<h3 id=\"h-get-profiles\" class=\"wp-block-heading\">GET Profiles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Earlier, we fetched a singular profile by its key. In this step, we&#8217;ll add logic to search through all profiles and return those that match a search string. Get user profiles using the <code>cluster.query()<\/code> method in the SDK and results are returned based on firstName or lastName with support to paginate results. Add the following to handle GET requests in <code>user.js<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>try {\n  const options = {\n    parameters: {\n      SKIP: Number(req.query.skip || 0),\n      LIMIT: Number(req.query.limit || 5),\n      SEARCH: req.query.search ? `%${req.query.search.toLowerCase()}%` : null\n    }\n  }\n\n  const query = options.parameters.SEARCH == null ? `\n      SELECT p.*\n      FROM ${process.env.CB_BUCKET}._default.profile p\n      LIMIT $LIMIT OFFSET $SKIP;\n      ` : `\n      SELECT p.*\n      FROM ${process.env.CB_BUCKET}._default.profile p\n      WHERE lower(p.firstName) LIKE $SEARCH OR lower(p.lastName) LIKE $SEARCH\n      LIMIT $LIMIT OFFSET $SKIP;\n    `\n  await cluster.query(query, options)\n      .then((result) =&gt; res.send(result.rows))\n      .catch((error) =&gt; res.status(500).send({\n        &quot;message&quot;: `Query failed: ${error.message}`\n      }))\n} catch (e) {\n  console.error(e)\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">his endpoint is different from the others as it makes a N1QL query rather than a key-value operation. This involves additional overhead because the query engine is involved. Remember that the <code>profileCollection<\/code> index (primary) was set up specifically to enable this endpoint.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Our <code>req.body<\/code> has three query params: <code>skip<\/code>, <code>limit<\/code>, and <code>search<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">We also have default values set up in case they are not provided, <code>0<\/code> for skip or <code>5<\/code> for limit. If the search string is not provided, it will default to null in which case the query string used will fetch all documents within the proflie collection.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Then, we build our N1QL query using the parameters we just created.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Finally, we pass that <code>query<\/code> and the <code>options<\/code> to the <code>cluster.query()<\/code> method and return the result.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Take notice of the N1QL syntax format and how it targets <code>bucket<\/code>.<code>scope<\/code>.<code>collection<\/code>.<\/p>\n\n\n\n<h4 id=\"h-search-profiles-ui\" class=\"wp-block-heading\">Search Profiles UI<\/h4>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">NOTE: The UI source code for this quickstart guide is quite a bit more complex (due to styling and layout) than the inline code blocks outlined in this tutorial. The functionality, however, is the same. The original code can be found in the <a href=\"https:\/\/github.com\/couchbase-examples\/nextjs-quickstart\/tree\/v1\">v1 branch<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p class=\"wp-block-paragraph\">Let&#8217;s add yet another form with a search field, and a flexbox to display results to enable easy search and retrieval of multiple profiles.<\/p>\n\n\n\n<pre class=\"wp-block-code language-html\"><code>&lt;form onSubmit={handleProfileSearch}&gt;\n    &lt;input type=&quot;text&quot; placeholder=&quot;Search String&quot; name=&quot;searchString&quot;\/&gt;\n    &lt;button type=&quot;submit&quot;&gt;Search&lt;\/button&gt;\n&lt;\/form&gt;\n\n&lt;h4&gt;Profile Search Results:&lt;\/h4&gt;\n&lt;div style={{ display: &quot;flex&quot; }}&gt;\n    {searchResults !== null &amp;&amp; searchResults.map((userProfile) =&gt; {\n      console.log(userProfile);\n      return (\n          &lt;UserCard firstName={userProfile.firstName} lastName={userProfile.lastName} email={userProfile.email} pid={userProfile.pid} allowDelete={true}\/&gt;\n      )\n    })\n    }\n&lt;\/div&gt;\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We can handle searches with a similar handler function to the POST and PUT\/Update buttons:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>  const handleProfileSearch = async (event) =&gt; {\n    event.preventDefault();\n\n    await fetch(`http:\/\/localhost:3000\/api\/user?search=${event.target.searchString.value}`, {\n      method: &#039;GET&#039;,\n    }).then(async (data) =&gt; {\n      setSearchResults(await data.json());\n    })\n  }\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">We&#8217;ll also need to add a state array inside the <code>Home()<\/code> function to keep track of the search results:<\/p>\n\n\n\n<pre class=\"wp-block-code language-js\"><code>const [searchResults, setSearchResults] = useState([]);\n<\/code><\/pre>\n\n\n\n<h3 id=\"h-notes-about-the-quickstart-code\" class=\"wp-block-heading\">Notes About the Quickstart Code<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\n<p>We&#8217;ve included a <code>.env.default<\/code> file which is used for testing and gitpod instances of the project to ensure smooth setup in these environments.<\/p>\n<\/li>\n\n\n<li>\n<p>In the completed quickstart code, fetch URLs use a dynamic <code>origin<\/code> variable instead of hard coding <code>http:\/\/localhost:3000<\/code> to ensure requests work when running in other environments.<\/p>\n<\/li>\n\n<\/ul>\n\n\n\n<h3 id=\"h-conclusion\" class=\"wp-block-heading\">Conclusion<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Next.js offers powerful tooling to create custom pages, components, and API endpoints. We&#8217;ve learned how to fetch data from within a page as well as add POST, PUT, GET, and DELETE routes to handle more complex backend logic from a single file (<code>user.js<\/code>). We also briefly touched on creating custom components that don&#8217;t render as pages but rather function as elements to build your pages effortlessly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Although this example is by no means a production ready web app, it should provide the necessary knowledge, and a great jumping off point for more complex applications.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Prerequisites To run this prebuilt project, you will need: What We&#8217;ll Cover Bootstrapping a new Next.js app using the &#8216;with-couchbase&#8217; example, building out a simple REST API using the API framework built into Next.js and the NodeJS SDK for Couchbase version 3.x with the following endpoints: We&#8217;ll also build out a basic front-end application with [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false,"inline_featured_image":false,"_acf":"","footnotes":""},"tutorial-languages":[653],"tutorial-tags":[664],"tutorial-technologies":[572,548,549],"tutorial-types":[555],"class_list":["post-10367","tutorials","type-tutorials","status-publish","hentry","tutorial-languages-nodejs","tutorial-tags-next-js","tutorial-technologies-index","tutorial-technologies-kv","tutorial-technologies-query","tutorial-types-tutorial"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v28.8 (Yoast SEO v28.9) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Using Next.js with Couchbase NodeJS SDK - Couchbase<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/\" \/>\n<meta property=\"og:locale\" content=\"it_IT\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using Next.js with Couchbase NodeJS SDK\" \/>\n<meta property=\"og:description\" content=\"Prerequisites To run this prebuilt project, you will need: What We&#8217;ll Cover Bootstrapping a new Next.js app using the &#8216;with-couchbase&#8217; example, building out a simple REST API using the API framework built into Next.js and the NodeJS SDK for Couchbase version 3.x with the following endpoints: We&#8217;ll also build out a basic front-end application with [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/\" \/>\n<meta property=\"og:site_name\" content=\"Couchbase\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/Couchbase\/\" \/>\n<meta property=\"og:image\" content=\"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:site\" content=\"@couchbase\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"18 minuti\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/\",\"name\":\"Using Next.js with Couchbase NodeJS SDK - Couchbase\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/da-demo-images.s3.amazonaws.com\\\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal\",\"datePublished\":\"2023-07-18T18:10:47+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/#breadcrumb\"},\"inLanguage\":\"it-IT\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/#primaryimage\",\"url\":\"https:\\\/\\\/da-demo-images.s3.amazonaws.com\\\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal\",\"contentUrl\":\"https:\\\/\\\/da-demo-images.s3.amazonaws.com\\\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/developers\\\/tutorials\\\/quickstart-nextjs\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.couchbase.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using Next.js with Couchbase NodeJS SDK\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#website\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/\",\"name\":\"Couchbase\",\"description\":\"The Operational Data Platform for AI\u2122\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#organization\"},\"alternateName\":\"Couchbase, Inc.\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"it-IT\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#organization\",\"name\":\"Couchbase\",\"alternateName\":\"Couchbase, Inc.\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"it-IT\",\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"contentUrl\":\"https:\\\/\\\/www.couchbase.com\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/Couchbase_Logo_Couchbase_Token_Red-1.png\",\"width\":1129,\"height\":1129,\"caption\":\"Couchbase\"},\"image\":{\"@id\":\"https:\\\/\\\/www.couchbase.com\\\/it\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/Couchbase\\\/\",\"https:\\\/\\\/x.com\\\/couchbase\",\"https:\\\/\\\/www.youtube.com\\\/c\\\/CouchbaseServer\",\"https:\\\/\\\/www.linkedin.com\\\/company\\\/couchbase\\\/\",\"https:\\\/\\\/github.com\\\/couchbase\\\/\",\"https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Couchbase,_Inc.\"],\"description\":\"Couchbase is a database software company headquartered in San Jose, California, providing an operational data platform for AI used by hundreds of global enterprises.\",\"legalName\":\"Couchbase, Inc.\",\"foundingDate\":\"2011-01-01\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Using Next.js with Couchbase NodeJS SDK - Couchbase","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/","og_locale":"it_IT","og_type":"article","og_title":"Using Next.js with Couchbase NodeJS SDK","og_description":"Prerequisites To run this prebuilt project, you will need: What We&#8217;ll Cover Bootstrapping a new Next.js app using the &#8216;with-couchbase&#8217; example, building out a simple REST API using the API framework built into Next.js and the NodeJS SDK for Couchbase version 3.x with the following endpoints: We&#8217;ll also build out a basic front-end application with [&hellip;]","og_url":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/","og_site_name":"Couchbase","article_publisher":"https:\/\/www.facebook.com\/Couchbase\/","og_image":[{"url":"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal","type":"","width":"","height":""}],"twitter_card":"summary_large_image","twitter_site":"@couchbase","twitter_misc":{"Est. reading time":"18 minuti"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/","url":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/","name":"Using Next.js with Couchbase NodeJS SDK - Couchbase","isPartOf":{"@id":"https:\/\/www.couchbase.com\/it\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/#primaryimage"},"image":{"@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/#primaryimage"},"thumbnailUrl":"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal","datePublished":"2023-07-18T18:10:47+00:00","breadcrumb":{"@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/#breadcrumb"},"inLanguage":"it-IT","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/"]}]},{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/#primaryimage","url":"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal","contentUrl":"https:\/\/da-demo-images.s3.amazonaws.com\/runItNow_outline.png?couchbase-example=nextjs-quickstart-repo&amp;source=devPortal"},{"@type":"BreadcrumbList","@id":"https:\/\/www.couchbase.com\/it\/developers\/tutorials\/quickstart-nextjs\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.couchbase.com\/"},{"@type":"ListItem","position":2,"name":"Using Next.js with Couchbase NodeJS SDK"}]},{"@type":"WebSite","@id":"https:\/\/www.couchbase.com\/it\/#website","url":"https:\/\/www.couchbase.com\/it\/","name":"Couchbase","description":"The Operational Data Platform for AI\u2122","publisher":{"@id":"https:\/\/www.couchbase.com\/it\/#organization"},"alternateName":"Couchbase, Inc.","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.couchbase.com\/it\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"it-IT"},{"@type":"Organization","@id":"https:\/\/www.couchbase.com\/it\/#organization","name":"Couchbase","alternateName":"Couchbase, Inc.","url":"https:\/\/www.couchbase.com\/it\/","logo":{"@type":"ImageObject","inLanguage":"it-IT","@id":"https:\/\/www.couchbase.com\/it\/#\/schema\/logo\/image\/","url":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","contentUrl":"https:\/\/www.couchbase.com\/wp-content\/uploads\/2026\/11\/Couchbase_Logo_Couchbase_Token_Red-1.png","width":1129,"height":1129,"caption":"Couchbase"},"image":{"@id":"https:\/\/www.couchbase.com\/it\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/Couchbase\/","https:\/\/x.com\/couchbase","https:\/\/www.youtube.com\/c\/CouchbaseServer","https:\/\/www.linkedin.com\/company\/couchbase\/","https:\/\/github.com\/couchbase\/","https:\/\/en.wikipedia.org\/wiki\/Couchbase,_Inc."],"description":"Couchbase \u00e8 una societ\u00e0 di software per database con sede a San Jose, in California, che fornisce una piattaforma operativa per i dati utilizzata da centinaia di aziende globali.","legalName":"Couchbase, Inc.","foundingDate":"2011-01-01"}]}},"acf":[],"permalink_manager":null,"_links":{"self":[{"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorials\/10367","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorials"}],"about":[{"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/types\/tutorials"}],"version-history":[{"count":0,"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorials\/10367\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/media?parent=10367"}],"wp:term":[{"taxonomy":"tutorial-languages","embeddable":true,"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorial-languages?post=10367"},{"taxonomy":"tutorial-tags","embeddable":true,"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorial-tags?post=10367"},{"taxonomy":"tutorial-technologies","embeddable":true,"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorial-technologies?post=10367"},{"taxonomy":"tutorial-types","embeddable":true,"href":"https:\/\/www.couchbase.com\/it\/wp-json\/wp\/v2\/tutorial-types?post=10367"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}