小编Joh*_*ohn的帖子

带 React-Query 的搜索框

我正在尝试通过 实现产品搜索text。使用 获取数据react-query。下面的实现是有效的,但我觉得不合适。让我知道我是否做得太过分,以及是否有更简单的解决方案react-query

import { useEffect, useState } from 'react';
import { useSelector } from 'react-redux';
import { useQueryClient } from 'react-query';
import ProductCard from '@/components/cards/ProductCard';
import { useQueryProducts } from '@/hooks/query/product';
import { selectSearch } from '@/store/search';

// function fetchProductsByFilter(text){}

const Shop = ({ count }) => {
  const [products, setProducts] = useState([]);
  const [loading, setLoading] = useState(true);

  const { text } = useSelector(selectSearch);

  const productsQuery = useQueryProducts(count);

  useEffect(() => {
    setProducts(productsQuery.data);
    setLoading(false); …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs next.js react-query

7
推荐指数
1
解决办法
2万
查看次数

如何处理函数数组中的重复函数调用?

我试图按顺序执行以下函数(同步/异步)数组(避免callbackHell),以实现函数runCallbacksInSequence(我需要实现自己的函数以了解回调如何工作并避免使用Async.js)。

这是我到目前为止所拥有的。该功能runCallbacksInSequence运行良好,直到callback多次获得相同的效果为止。它停止并且不再继续执行下一个回调。理想情况下,如果callback多次获得相同的结果,则不应第二次执行,而应继续执行下一次callback

如果您有任何想法,请告诉我我做错了什么以及如何解决。 -没有承诺和异步/等待

function first(cb) {
  setTimeout(function() {
    console.log('first()');
    cb(null, 'one');
  }, 0);
}

function second(cb) {
  setTimeout(function() {
    console.log('second()');
    cb(null, 'two');
  }, 100);
}

function third(cb) {
  setTimeout(function() {
    console.log('third()');
    cb(null, 'three');
  }, 0);
}

function last(cb) {
  console.log('last()');
  cb(null, 'lastCall');
}

const cache = {};

function runCallbacksInSequence(fns, cb) {
  fns.reduce(
    function(r, f) {
      return function(k) {
        return r(function() {
          if (cache[f]) {
            return;
            // f(function(e, x) { …
Run Code Online (Sandbox Code Playgroud)

javascript

6
推荐指数
1
解决办法
166
查看次数

使用 Mongoose 从 Decimal128 中提取十进制 - MongoDB

我正在使用 Mongoose 在 Nodejs 中查询 Mongo 并尝试提取存储为 Decimal128 的多个字段的数值。但是,该值奇怪地包含在查询结果中,我不确定如何通过 Mongo 或 Mongoose 提取它:

{data:[
  {
  "date": {
          "$numberDecimal": "1530057600000"
  },
  "open": {
          "$numberDecimal": "86.13"
  },
  "high": {
          "$numberDecimal": "86.63"
  },
  "low": {
          "$numberDecimal": "85.47"
  },
  "close": {
          "$numberDecimal": "85.64"
  },
  "volume": {
          "$numberDecimal": "308508"
  }
},
Run Code Online (Sandbox Code Playgroud)

有没有办法可以使用 Mongo 或 Mongoose 将上面的 JSON 查询结果转换为下面的内容?

{data:[
 {
  "date": 1530057600000
  "open": 86.13
  "high": 86.63
  "low": 85.47
  "close": 85.64
  "volume": 308508
 },
Run Code Online (Sandbox Code Playgroud)

我尝试按如下方式选择字段,但这不起作用。

    data[i].date.$numberDecimal, 
    data[i].open.$numberDecimal,
    data[i].high.$numberDecimal,
    data[i].low.$numberDecimal, 
    data[i].close.$numberDecimal 
Run Code Online (Sandbox Code Playgroud)

这是我的猫鼬模式:

文件夹 - 模型 - Stock.js …

javascript mongoose mongodb node.js

5
推荐指数
2
解决办法
1万
查看次数

在 Next.js api 文件夹中使用 noookies 设置 cookie

我正在尝试使用 Next.js api 文件夹使用 npm 包设置 cookie nookies。我已经使用了类似的代码来设置 cookie,并且nookies效果很好。由于某种原因,它在这种情况下不起作用。我不确定我做错了什么。如果您有任何想法,请告诉我。

import { parseCookies, setCookie } from 'nookies';
import { paymentIntent } from '@/Models/Stripe';
import { cartByUser } from '@/Models/Cart';
import { currentUser } from '@/Models/User';

export const createPaymentIntentController = async (req, res) => {
  const { email } = req.user;
  const { couponApplied } = req.body;

  const { appPaymentIntentId } = parseCookies({req});
  console.log({ appPaymentIntentId });

  try {
    const user = await currentUser(email);
    const cart = await cartByUser(user._id);
    const …
Run Code Online (Sandbox Code Playgroud)

javascript node.js next.js

5
推荐指数
1
解决办法
5231
查看次数

如何延迟/开始/去抖动获取数据直到用户停止输入?

如果我的应用程序有足够多的用户,每次击键时发送一个 ajax 请求是让服务器瘫痪的有效方法(更不用说可能使客户端应用程序感觉很慢)。关于实现带有两个选项(DB 搜索和 Web Api 搜索)的符号搜索框。当我在搜索框中输入符号(例如:AAPL - aple stock)时,fetch()每次都会通过网络发送请求。为了避免它,我尝试使用,setTimeout()fetch()无论如何都会多次发送请求。如何延迟/开始/去抖动获取请求,直到用户停止在输入区域输入以仅发送一个fetch()请求?

HTML:

<label for="symbolTags">Symbol: </label>
  <input type="text" id="symbolTags" name="symbol">

  <label for="api">Select Search Api: </label>
  <select id="api" name="routes_api">
    <option value="search">Web Search Api</option>
    <option value="dbsearch">DB Search Api</option>
  </select>
Run Code Online (Sandbox Code Playgroud)

JavaScript:

const symbolTags = document.querySelector('#symbolTags')
const symbolTagsOptions = document.querySelector('#api')

const urlsObject = {
  dbsearch: '/dbsearch/',
  search: '/search/'
}

symbolTags.oninput = function () {
  let symbolTagsOptionsValue = symbolTagsOptions.value
  let arg = urlsObject[symbolTagsOptionsValue]

  // Init a timeout variable to …
Run Code Online (Sandbox Code Playgroud)

javascript node.js

4
推荐指数
1
解决办法
1672
查看次数

How to import socket.io npm packege - Nodejs

Any idea why I am getting an error?

  import Server from 'socket.io';
  const socketio = new Server();
Run Code Online (Sandbox Code Playgroud)

Error

import Server from 'socket.io';
       ^^^^^^
SyntaxError: The requested module 'socket.io' does not provide an export named 'default'
Run Code Online (Sandbox Code Playgroud)

javascript node.js socket.io

4
推荐指数
1
解决办法
2271
查看次数

比较 JavaScript 中的循环(自引用)对象

我比较包含值两个对象stringnumberarrayobject。到此为止没有问题。当我尝试比较自引用对象时,出现以下错误RangeError: Maximum call stack size exceeded。如果自引用对象被其他对象的同一级别引用,则应认为它们是相等的。我的问题是如何实现它。这是我的代码:

const equalsComplex = function(value, other) {
  // Get the value type
  const type = Object.prototype.toString.call(value);

  // If the two objects are not the same type, return false
  if (type !== Object.prototype.toString.call(other)) return false;

  // If items are not an object or array, return false
  if (['[object Array]', '[object Object]'].indexOf(type) < 0) return false;

  // Compare the length of the length of the two items …
Run Code Online (Sandbox Code Playgroud)

javascript

2
推荐指数
1
解决办法
615
查看次数

How to import Socket.io using import - Nodejs

Not sure why I am getting the error. When I try to do the same thing by using request it works fine.

import express from 'express';
import { createServer } from 'http';
import * as io from 'socket.io';

const app = express();
const server = createServer(app);
const socketio = io(server);
Run Code Online (Sandbox Code Playgroud)

Error

const socketio = io(server);
                 ^

TypeError: io is not a function
Run Code Online (Sandbox Code Playgroud)

javascript node.js socket.io

0
推荐指数
1
解决办法
1259
查看次数