根据文档:
path.join() 方法使用平台特定的分隔符作为分隔符将所有给定的路径段连接在一起,然后规范化结果路径。
零长度路径段将被忽略。如果连接的路径字符串是零长度字符串,则为“.” 将返回,代表当前工作目录。
Run Code Online (Sandbox Code Playgroud)path.join('/foo', 'bar', 'baz/asdf', 'quux', '..'); // Returns: '/foo/bar/baz/asdf' path.join('foo', {}, 'bar'); // Throws 'TypeError: Path must be a string. Received {}'如果任何路径段不是字符串,则会引发 TypeError。
我错过了什么吗?为什么是:
path.join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// Returns: '/foo/bar/baz/asdf'
Run Code Online (Sandbox Code Playgroud)
忽略'quux'和'..'?
它们的长度不为零?
甚至在 REPL 中玩过(参见屏幕截图)
我正在使用 Next.js 并想添加react-semantic-ui,以使用他们的登录组件之一。
在前端,我收到此错误:无法编译
./node_modules/semantic-ui-css/semantic.min.css
ModuleParseError: Module parse failed: Unexpected character '' (1:0)
You may need an appropriate loader to handle this file type.
(Source code omitted for this binary file)
Run Code Online (Sandbox Code Playgroud)
这是登录组件:
import React from 'react'
import { Button, Form, Grid, Header, Image, Message, Segment } from 'semantic-ui-react'
const Login = () => (
/* login JSX markup */
)
export default Login
Run Code Online (Sandbox Code Playgroud)
这是我的 next.config.js
module.exports = {
webpack: (config, { dev }) => {
config.module.rules.push(
{
test: …Run Code Online (Sandbox Code Playgroud) 我已经设置了一个 Provider 来为使用useContext. 因此,我试图利用本地存储为用户图像(头像等)保存一些状态。
首先,我试图为用户保留头像,本质上是从 express 保存他们的 ID,然后在我调用 Cloudinary(一种图像托管服务)时使用它。
我想我很接近(因为我获得了头像的默认图像占位符),但我无法设置本地存储。
import React, { useState, useEffect } from 'react';
import dynamic from 'next/dynamic';
var initialState = {
userId: null,
avatar: '/static/uploads/profile-avatars/placeholder.jpg'
};
var UserContext = React.createContext();
function getLocalStorage() {
return Object.keys(initialState).forEach(item => {
dynamic(
() =>
Object.keys(initialState).forEach(
val => (initialState[window.localStorage.getItem(item)] = val)
),
{
ssr: false
}
);
});
}
function setLocalStorage() {
Object.keys(initialState).forEach(item => {
console.log('item setLocalStorage', item);
dynamic(
() =>
window.localStorage.setItem(
item,
Object.values(initialState).forEach(item => item)
),
{ …Run Code Online (Sandbox Code Playgroud) 我正在玩"ID"试图让自己变得舒适等等.我知道jQuery有一个非常快速的解决方案,但我想知道如何通过js来做到这一点.
var startTimer = function init(){
var n = 0;
var e = document.getElementById("output");
var myTimer = setInterval (function() {e.innerHTML = n++;},100);
// clearInterval(myTimer);
if(myTimer === 10){
clearInterval(myTimer);
return;
}
}
startTimer();
Run Code Online (Sandbox Code Playgroud) 我正在阅读"JavaScriptAllongé",并在有关功能的部分中看到了这一点.
function (value) {
return (function (copy) {
return copy === value
})(value)
}(value);
Run Code Online (Sandbox Code Playgroud)
假设我传递5 ...一个字符串,一个函数,一个数组或ANYTHING作为参数!为什么评价为真?似乎什么都没有传递给参数副本什么都没有?我唯一能想到的是,当你将5传递给value时,它也被丢弃在return函数的参数中?
谢谢!
听说最好的做法是清理前端和后端的数据。
我在前端进行了一些验证(即检查是否通过 Regex 发送电子邮件并确保密码具有特定长度)。
但是现在我想使用自定义验证器来检查我的 mongodb 数据库中是否存在电子邮件。
.post(body('username').custom(value => {
UserModel.findOne({ 'email': value }).then(user => {
if (user) {
return Promise.reject('E-mail already in use');
}
});
}),
Run Code Online (Sandbox Code Playgroud)
这是我的其余代码:
var router = require('express').Router()
var UserModel = require('../models/UserModel')
var { body } = require('express-validator');
router
.route('/registration')
.get(function(req, res) {
UserModel.find({}, (err, users) => {
if (err) res.status(500).send(err)
res.json(users)
})
})
.post(body('username').custom(value => {
UserModel.findOne({ 'email': value }).then(user => {
if (user) …Run Code Online (Sandbox Code Playgroud) 我遇到了这个中间人包并尝试运行它,bundle exec middleman server但在终端中取回...
The source :rubygems is deprecated because HTTP requests are insecure. Please change your source to 'https://rubygems.org' if possible, or 'http://rubygems.org' if not.
== The Middleman is loading
== Port 4567 is unavailable. Either close the instance of Middleman already running on 4567 or start this Middleman on a new port with: --port=4568
Run Code Online (Sandbox Code Playgroud)
提前致谢!
我正在审查函数与全局范围中的变量可访问性,并遇到了一些新的东西,并想知道以下是否正确:
function foo (){
message='Hi'
}
alert(message); //ReferenceError: message is not defined
Run Code Online (Sandbox Code Playgroud)
这会返回一个错误,因为foo尚未在任何地方调用,但是一旦执行,它就可以在全局空间中使用.得到它了.
然而,除了明确地使函数返回一个值(即使用return关键字)之外,我在某处阅读了我们使用它们的副作用(例如,想象一下我将该警报函数放在foo中我刚引用).
以上是以某种方式以与使用return关键字时相同的方式返回值?
更新 我收到了很多反馈,但也许我应该重申一下这个问题.仅仅是调用函数将其置于全局范围内的事实.就像幕后发生的一样......?我认为有些人可能会认为(就像我一样)只是省略var,它就在全球范围内.实际上,对于那些不知道的人,你必须先调用该函数.谢谢!
javascript ×3
next.js ×2
node.js ×2
reactjs ×2
express ×1
middleman ×1
mongoose ×1
react-hooks ×1
use-context ×1
use-state ×1