Git Product home page Git Product logo

Comments (2)

VR-Architect avatar VR-Architect commented on May 24, 2024

Also, when the API doesn't receive a username the code crashes.

Error:

"MSG: D1_ERROR, Stack:Error: D1_ERROR\n    at D1Database._send (d1-beta-facade.entry.js:2113:13)\n    at async D1PreparedStatement.all (d1-beta-facade.entry.js:2151:7)\n    at async user_search (d1-beta-facade.entry.js:1962:19)\n    at async d1-beta-facade.entry.js:2017:19\n    at async d1-beta-facade.entry.js:1435:7\n    at async d1-beta-facade.entry.js:1375:50"

Here is my code for review:

import { D1QB, OrderTypes } from 'workers-qb'

export async function user_search(c) {
    // page         integer Which page of results to return (! zero-based !)
    // per_page	    integer	How many results to return per page
    // order	    string	Attribute/field name to order the responses by
    // direction    string	Either or ASC or DESC
    const { page, per_page, order_by, direction, username } = await c.req.json()

    // Validate input
    var limit, offset;
    if (isNumeric(per_page)) {
        limit = per_page;
    }
    if (isNumeric(page)) {
        offset = per_page * page;
    }
    const conditions = []
    if (username) {
        conditions.push('Username = ?1')
    }

    const qb = new D1QB(c.env.DB)

    var fields = ['Username, Active, EmailVerified, RealName, Perms, Created_on, Modified_on, Modified_by']

    const fetched = await qb.fetchAll({
        tableName: 'Users',
        fields: fields,
        where: {
            conditions: conditions,
            params: [username],
        },
        //orderBy: { Username: OrderTypes.ASC },
        limit: limit,
        offset: offset
    })
    return fetched.results;
}

function checkIfEmpty(str) {
    if (typeof str === 'string' || str instanceof String) {
        if (!str?.trim() || str.trim().length === 0) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

function isNumeric(str) {
    if (typeof str == "number") { return true; }
    if (typeof str != "string") return false // we only process strings!  
    return !isNaN(str) && // use type coercion to parse the _entirety_ of the string (`parseFloat` alone does not do this)...
        !isNaN(parseFloat(str)) // ...and ensure strings of whitespace fail
}

from workers-qb.

G4brym avatar G4brym commented on May 24, 2024

Hey @VR-Architect that api crash also seams to be on Cloudflare side, if that still happens maybe you should report that on their Discord Server

from workers-qb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.