小编vam*_*ire的帖子

Object.keys()从集合中返回MongoDB对象上的意外键

在这里处理一个奇怪的问题.这是一个从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)

javascript mongoose node.js

10
推荐指数
1
解决办法
5386
查看次数

开玩笑嘲笑/监视 Mongoose 链式(查找、排序、限制、跳过)方法

我想要做什么:

  • 监视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');
  • 研究模拟链式方法调用但没有成功

unit-testing mocking chained mongoose jestjs

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

Visual Studio代码-将模型导入另一个文件后,无法识别猫鼬(自定义)架构方法

预期功能:对象方法的自动完成和方法参数的识别

实际功能:无法识别自动完成或方法参数。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)

javascript mongoose node.js visual-studio-code

6
推荐指数
0
解决办法
926
查看次数

在@Document indexName 中使用的 SpEL 与 spring 数据 elasticsearch 和 spring boot 没有被解析

寻找使用 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}"作为索引名称!

我也试过创建一个@ComponentEsConfig

带有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

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

JavaScript:数组.为什么将带有填充数组的空数组连接成字符串?

我正在解决扁平化数组的问题.我遇到了一些非常奇怪的东西,似乎无法在网上找到答案.

为什么

[] + [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会将'[]'设置为'初始值',因此对于原始数组中的每个元素,它会将它与一个空数组连接,从而"展平"数组.

javascript arrays concatenation

4
推荐指数
2
解决办法
3906
查看次数

从 mac 命令行将 .sql 文件导入 mysql。试过 mysql -u root -p db_name > path/to/dbfile.sql

我尝试从命令行执行此操作

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)

mysql bash zsh

3
推荐指数
1
解决办法
9157
查看次数

这个语法叫什么?(int)(value)与int(value)相同

我在一个练习题中看到了这个,并且之前从未见过Python中使用的这种语法.没有任何运气谷歌搜索它

python syntax python-3.x

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