在这里处理一个奇怪的问题.这是一个从mongodb中提取并传递给以下函数的对象数组.
我forEach从数据库中拉出的数组中依次尝试了以下3个日志:
e(正确返回的数组中的object元素).如您所见,所有属性(键)都存在:{ paid: false,
hotelWebsite: 'www.testing.com',
_id:5951848a24bb261eed09d638,
hotelAddress: '123 easy street',
...etc }
Run Code Online (Sandbox Code Playgroud)
console.log(Object.keys(e)) 正在回归不是关键的东西......[ '__parentArray',
'__parent',
'__index',
'$__',
'isNew',
'errors',
'_doc',
'$init' ]
Run Code Online (Sandbox Code Playgroud)
for(key in e){
console.log(key);
}
Run Code Online (Sandbox Code Playgroud)
它返回绝对混乱的数据,其中一部分包含对象的实际键:
__parentArray
__parent
__index
$__
isNew
errors
_doc
$init
id
_id
hotelWebsite
hotelAddress
hotelNumber
hotelName
courseCost
courseDate
courseState
courseCity
courseName
paid
studentComments
studentEmail
studentPhone
studentCountry
studentZip
studentState
studentCity
studentAddress
studentCompany
studentName
schema
constructor
$__original_remove
remove
_pres
_posts
$__original_validate
validate
toBSON
markModified
populate
save
update …Run Code Online (Sandbox Code Playgroud) find()静态模型方法定义中使用的
链接方法调用sort(), limit(),skip()目标:监视传递给静态模型方法定义中每个方法的参数:
... 静态方法定义
const results = wait this.find({}).sort({}).limit().skip();
... 静态方法定义
find()收到的参数是什么:完成了findSpy
sort()收到的参数是什么:不完整limit()收到的参数是什么:不完整skip()收到的参数是什么:不完整mockingoose,但仅限于find()find()方法本身,但不能模拟其之后的链式调用
const findSpy = jest.spyOn(models.ModelName, 'find');预期功能:对象方法的自动完成和方法参数的识别
实际功能:无法识别自动完成或方法参数。any将鼠标悬停在工具提示上的方法显示上(而不是该方法的父对象)
有什么帮助吗?我今天刚从Webstorm切换到VSC,遇到了这个问题。
结构和代码
自定义静态方法(导出userProfile模型):
/database/profileModel.js
userSchema.statics.addProfile = function(formData){
this.create(formData, e => e ? console.log(e) : false);
};
userSchema.statics.getProfile = function(userName){
return this.findOne({userName : userName});
};
userSchema.statics.getProfileItem = function(userName, item){
return this.findOne({userName : userName}, item);
};
Run Code Online (Sandbox Code Playgroud)
API端点(导出路由器):/routes/APIendpoint.js
const express = require('express');
const router = module.exports = express.Router();
const userProfile = require('../database/profileModel').userProfile;
userProfile.getProfileItem()
Run Code Online (Sandbox Code Playgroud)
@Neil Lunn这是它起作用的证明-因为您非常确定。 没有高级模式
同样,这也不能解决问题。这是使用高级架构的新代码。它识别该文件中的方法,但不能识别导入该文件的其他文件中的方法。
class User {
// general static methods
addProfile(formData) {
this.create(formData, error => console.log(error));
}
getProfile(userName) {
return …Run Code Online (Sandbox Code Playgroud) 寻找使用 SpEL 内部@Document注释的一些帮助,参考:
spring-data-elasticsearch:3.2.3.RELEASE 和弹簧靴 2.2.1 RELEASE
我在用谷歌搜索帮助解决这个问题时遇到了麻烦,因为关键字选择了不相关的问题(我已经看到了关于动态 indexName的另一个(未回答的)问题)。
我想设置
@Document(indexName = "${es.index-name}", ...)
用my 中写入indexName的属性 ( es.index-name) 值派生的值application.properties。
它改为使用文字字符串值"${es.index-name}"作为索引名称!
我也试过创建一个@Component叫EsConfig
带有indexName注释的字段@Value("${es.index-name}")
然后尝试使用 SpEL 访问此组件属性值:
@Document(indexName = "#{esConfig.indexName}", ...)
但这也不起作用(仍然解析为文字字符串并抱怨大写)。我已经通过调试器确认EsConfig组件正在正确解析 SpEL 并提供正确的值。但到达时失败@Document
以下是完整的代码片段:
利用@Document与规划环境地政司访问application.properties
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Data
@Document(indexName = "${es.index-name}", type = "tests")
public class TestDocument { …Run Code Online (Sandbox Code Playgroud) spring-annotations spring-el spring-boot spring-data-elasticsearch
我正在解决扁平化数组的问题.我遇到了一些非常奇怪的东西,似乎无法在网上找到答案.
为什么
[] + [1,2] = '1,2'
Run Code Online (Sandbox Code Playgroud)
我似乎无法理解为什么在填充的数组中添加一个空数组会产生一个包含填充数组内容的字符串.
背后会发生什么导致这种情况?
我的代码示例:
arr = [1, [2], [3, 4]];
arr.reduce(flatten, []); // [1, 2, 3, 4]
function flatten(a, b) {
return a.concat(b);
}
Run Code Online (Sandbox Code Playgroud)
据我所知,reduce会将'[]'设置为'初始值',因此对于原始数组中的每个元素,它会将它与一个空数组连接,从而"展平"数组.
我尝试从命令行执行此操作
mysql -u root -p db_name > ~/Documents/db_name.sql
Run Code Online (Sandbox Code Playgroud)
我试过从 mysqlimport 做这件事
mysqlimport -u root -p db_name ~/Documents/db_name.sql
Run Code Online (Sandbox Code Playgroud)
我只使用文件名在正确的目录中尝试了这两种方法。
我试过使用进入 mysql
mysql -u root -p
use db_name;
source ~/Documents/db_name.sql;
(nothing happens - no response)
(tried with absolute path - no response)
\. ~/Documents/db_name.sql
(nothing happens)
Run Code Online (Sandbox Code Playgroud)
我觉得我错过了一些东西。根据最近 30 分钟的谷歌搜索和尝试,这似乎是一个微不足道的操作。
最终,我不得不将整个 .sql 文件复制并粘贴到 mysql shell 中,同时使用正确的 db。
我觉得自己像个穴居人。请帮忙。
编辑:SQL 文件内容
-- phpMyAdmin SQL Dump
-- version 4.4.15.5
-- http://www.phpmyadmin.net
--
-- Host: 127.0.0.1:8889
-- Generation Time: May 09, 2017 at 09:27 PM
-- …Run Code Online (Sandbox Code Playgroud) 我在一个练习题中看到了这个,并且之前从未见过Python中使用的这种语法.没有任何运气谷歌搜索它
javascript ×3
mongoose ×3
node.js ×2
arrays ×1
bash ×1
chained ×1
jestjs ×1
mocking ×1
mysql ×1
python ×1
python-3.x ×1
spring-boot ×1
spring-el ×1
syntax ×1
unit-testing ×1
zsh ×1