I am wroking on a next js application with postgres. I am encountering an error
SyntaxError: Unexpected end of JSON input
at JSON.parse (<anonymous>)
at /home/sahood/Desktop/final-invoice-1/node_modules/@prisma/client/runtime/index.js:28174:31
at processTicksAndRejections (node:internal/process/task_queues:96:5)
I found that the error is occured from this page
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../../shared/services/prisma.service';
import * as fs from 'fs';
import * as path from 'path';
import { Template } from '@prisma/client';
import * as get from 'lodash/get';
import * as isEmpty from 'lodash/isEmpty';
const TEMPLATES_DIR = '/templates/';
const TEMPLATES_BASE = path.join(process.cwd(), TEMPLATES_DIR);
// getDirectories function
function getDirectories(srcpath) {
return fs
.readdirSync(srcpath)
.map((file) => path.join(srcpath, file))
.filter((path) => fs.statSync(path).isDirectory());
}
// getFiles function
function getFiles(srcpath) {
return fs
.readdirSync(srcpath)
.map((file) => path.join(srcpath, file))
.filter((path) => fs.statSync(path).isFile())
.map((file) => file.replace(/\.[^/.]+$/, ''));
}
@Injectable()
export class TemplateService {
constructor(public readonly prisma: PrismaService) {}
async onModuleInit() {
await this._initializeMeta();
await this.getTemplateTypes();
}
async initializeDefaults(shopName: string) {
const templateTypes = await this.getTemplateTypes();
await Promise.all(
templateTypes.map(async (type) => {
const handle = type.handle;
const defaultSlug = type.meta.default;
await this.initializeTemplateConfig(shopName, handle, defaultSlug);
await this.changeSelectedTemplate(shopName, handle, defaultSlug);
}),
);
}
getTemplatesByType(type: string) {
return this.prisma.template.findMany({
where: {
type,
},
});
}
getTemplateByTypeAndSlug(type: string, slug: string) {
return this.prisma.template.findUnique({
where: {
slug_type: { type, slug },
},
});
}
async getSelectedTemplate(shopName: string, type: string): Promise<Template> {
const selectedTemplate = await this.prisma.shop.findUnique({
where: {
name: shopName,
},
include: {
shopTemplate: {
where: {
templateType: type,
},
include: {
template: true,
},
},
},
});
return get(selectedTemplate, 'shopTemplate[0].template', null);
}
async changeSelectedTemplate(
shopName: string,
type: string,
slug: string,
): Promise<Template> {
const shop = await this.prisma.shop.findUnique({
where: {
name: shopName,
},
include: {
shopTemplate: true,
},
});
const shopTemplate = await this.prisma.shopTemplate.upsert({
where: {
shopId_templateType: {
shopId: shop.id,
templateType: type,
},
},
include: {
template: true,
},
create: {
shopId: shop.id,
templateType: type,
templateSlug: slug,
},
update: {
templateSlug: slug,
},
});
return get(shopTemplate, 'template', null);
}
async getTemplateTypes() {
const dirs = getDirectories(TEMPLATES_BASE);
const types = await Promise.all(
dirs.map(async (typePath) => {
const metaPath = path.join(typePath, 'meta.js');
const meta = await import(metaPath);
return {
handle: path.basename(typePath),
meta,
};
}),
);
return types;
}
async getMetaOfType(type: string) {
const templatePath = path.join(TEMPLATES_BASE, type);
const metaPath = path.join(templatePath, 'meta.js');
const meta = await import(metaPath);
return {
handle: type,
meta,
};
}
async getTemplateConfig(shopName: string, type: string, slug: string) {
const { meta } = await this.getMetaOfType(type);
const templateConfig = await this.prisma.shop.findUnique({
where: {
name: shopName,
},
include: {
shopTemplateConfig: {
where: {
templateType: type,
templateSlug: slug,
},
},
},
});
// adding meta.js panel handles if it is new and does not exist in the db data
for (const panel of meta.panels) {
if (templateConfig.shopTemplateConfig[0]?.config[panel.handle]) continue;
try {
templateConfig.shopTemplateConfig[0].config[panel.handle] = panel.state;
} catch (error) {
// console.log(error);
}
}
return get(templateConfig, 'shopTemplateConfig[0]', null);
}
async setTemplateConfig(
shopName: string,
type: string,
slug: string,
config,
): Promise<Template> {
const shop = await this.prisma.shop.findUnique({
where: {
name: shopName,
},
include: {
shopTemplateConfig: true,
},
});
const shopTemplate = await this.prisma.shopTemplateConfig.upsert({
where: {
shopId_templateType_templateSlug: {
shopId: shop.id,
templateType: type,
templateSlug: slug,
},
},
include: {
template: true,
},
create: {
shopId: shop.id,
templateType: type,
templateSlug: slug,
config: config,
},
update: {
config: config,
},
});
return get(shopTemplate, 'template', null);
}
async initializeTemplateConfig(shopName: string, type: string, slug: string) {
const { meta } = await this.getMetaOfType(type);
const initialConfig = {};
for (const panel of meta.panels) {
initialConfig[panel.handle] = panel.state;
}
await this.setTemplateConfig(shopName, type, slug, initialConfig);
return initialConfig;
}
_getTemplatePath(type: string, fileName: string) {
return path.join(TEMPLATES_BASE, type, 'styles', fileName);
}
_getLiquidPath(type: string) {
return path.join(TEMPLATES_BASE, type, 'index.liquid');
}
async _getTemplateMeta(type: string, slug: string) {
try {
const meta = this._getTemplatePath(type, `${slug}.js`);
const data = await import(meta);
return data;
} catch (error) {
return {};
}
}
async _initializeMeta() {
for (const templateTypePath of getDirectories(TEMPLATES_BASE)) {
const type = path.basename(templateTypePath);
const basePath = path.join(TEMPLATES_BASE, type, 'styles');
if (!fs.existsSync(basePath)) {
continue;
}
console.log(getFiles(basePath))
// where I applied getFiles function
for (const templatePath of getFiles(basePath)) {
const templateSlug = path.basename(templatePath);
console.log(templateSlug)
const meta = await this._getTemplateMeta(type, templateSlug);
if (isEmpty(meta)) {
console.warn(`✘ ${type}/${templateSlug} : meta.js not found`);
continue;
}
console.log(`✔ ${type}/${templateSlug} : meta initialized`);
console.log(meta)
await this.prisma.template.upsert({
where: {
slug_type: { type, slug: templateSlug },
},
update: {
...meta,
},
create: {
type,
slug: templateSlug,
...meta,
},
});
console.log(meta)
}
}
}
}
The error is happening when I replace getDirectories fuction with getFiles in only one place, to ensure update the data every time the change occurred in parameter data
And that error is only shown on my device, except it works fine on my college devices.
I have tried to delete node modules, .next folder and dist folder.I restarted the device, i cloned again and none of it worked.