代码帮助,以防止在wix数据库csv文件上传中重复记录 [英] Code help to prevent duplicate recording in wix database csv file upload

查看:56
本文介绍了代码帮助,以防止在wix数据库csv文件上传中重复记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码帮助,以防止在wix数据库csv文件上传中重复记录.

Code help to prevent duplicate recording in wix database csv file upload.

我无法运行下面的代码.为什么?

I can't run the code below. Why?

数据库:AmericanSamoa,检查重复的列:originalConnectionDetails

Database: AmericanSamoa, Check duplicate column : originalConnectionDetails

通过检查数据库中的原始链接URL地址,如果记录以前是用csv文件加载的,我想防止重复的条目.

By checking the original link url address in the database, I want to prevent duplicate entries if the record was previously loaded with csv file.

我的后端代码

import wixData from 'wix-data';
import request from "request-promise";



export async function AmericanSamoa_beforeInsert(item, context) {

 if (item.originalConnectionDetails) {        
        item.originalConnectionDetails= await wixData.query('AmericanSamoa').ne('_id', 
item._id).startsWith('originalConnectionDetails', item.originalConnectionDetails).find().then((x) => 
{
 const total = x.totalCount;
 if (total > 0) {
 return `${item.originalConnectionDetails}-${total+1}`;
            } else {
 return item.originalConnectionDetails;
            }
        })
    }

 return item
}

推荐答案

解决方案!

---此代码可防止在csv文件上传期间重复记录(如果之前已将其写入数据库).

---This code prevents duplicate records (if they were written to the database before) during csv file upload.

但是,应该不可能在添加的csv文件中重复记录.因为第一次上载不会检查文件中的重复记录.它会检查数据库中的csv文件并上传.

However, it should not be possible to record repeatedly in the csv file you added. Because the first time uploaded does not check the duplicate records in the file. It checks the csv file in the database and uploaded.

后端代码

import wixData from 'wix-data';

export function searchForDuplicates(value) {
 return wixData.query("Afghanistan")
   .eq("originalConnectionDetails", value)
   .find()
   .then((results) => {
 return results.items.length;
    })
    .catch((err) => {
 let errorMsg = err;
    });
}

export function Afghanistan_beforeInsert(item) {
 return searchForDuplicates(item.originalConnectionDetails).then((res) => {
 if(res > 0) {   
 return Promise.reject("Duplicate");
    }
 return item;
  });
}

这篇关于代码帮助,以防止在wix数据库csv文件上传中重复记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆