我在mongo中有这个项目:
[ { title: 'Product Name',
_id: 5052843e023273693300013c,
description: 'This is a fake description',
categories: [ 5052843e023273693300010a ],
} ]
Run Code Online (Sandbox Code Playgroud)
我想找到这样的产品.我试过了:
Product.find({ categories: mongoose.Types.ObjectId('5052843e023273693300010a')})
Product.find({ categories: mongoose.mongo.BSONPure.ObjectID.fromString('5052843e023273693300010a')})
Product.find({ categories: '5052843e023273693300010a'})
Product.find({ 'categories': '5052843e023273693300010a'})
Product.find({ categories: {$in: ['5052843e023273693300010a']}})
Product.find({ categories: Schema.Types.ObjectId('5052843e023273693300010a')})
Run Code Online (Sandbox Code Playgroud)
但没有任何作用.我可以使用id获取id:_id:'5052843e023273693300013c'.
请注意,当插入产品时,类别ID被添加为字符串(意味着我只是分配了ID而不是类别对象,但这并不能解释为什么上述工作都没有 - 它在转储中没有引用,所以也许Mongo认为是对象ID.
关于SO的类似问题没有得出答案.
我正在使用最新的Mongoose(3个东西)和最近的Mongo,Node.
更新:
我可以使用以下命令从CLI获取:
db.products.find({ categories: '5052843e02327369330000fe' });
Run Code Online (Sandbox Code Playgroud)
有趣的是,我可以通过在代码中执行不相等来获取它 - 是吗?:
Product.find({ categories: { $ne: '5052843e02327369330000fe' }})
Run Code Online (Sandbox Code Playgroud)
我的架构如下:
var Product = new Schema({
title: { type: String, required: true },
slug: { type: String },
summary: …Run Code Online (Sandbox Code Playgroud) 我使用的是express-validator版本2.3.0.似乎总是需要字段
req.check('notexist', 'This failed').isInt();
Run Code Online (Sandbox Code Playgroud)
永远都会失败 - 破碎还是我错过了什么?有一个notEmpty必填字段的方法似乎表明默认是可选的,但我无法通过上述方法.
我如何结束Express 3请求?res.end()肯定不会停止处理.
exports.home = function(req, res) {
res.end();
console.log('Still going, going...');
Run Code Online (Sandbox Code Playgroud) 我想让我的角应用程序执行$ http.get请求.IE8无处不在.我正在使用最新的Angular(我认为是1.0.6).IE给出的唯一消息是:TypeError:
访问被拒绝.undefined(以前的角度是这个)
我的主机(nodejs)设置为发送cors标头:
res.header('Access-Control-Allow-Origin', '*'); //config.allowedDomains
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Requested-By');
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
Run Code Online (Sandbox Code Playgroud)
我试图使用调试工具获取有关错误的更多信息,但是:
Fiddler:没有显示xmlhttprequests - 似乎你可以破解你的NET应用程序来修复它但不知道如何处理我的网页.DebugBar:没有显示这些请求FirebugLite:我试过的2版本被破坏 - 只是随机错误而且没有加载
将主机更改为相同的源可以解决问题所以我认为它正在尝试一些东西,但是人类很难调试.
关于这个的角度帖子似乎已经过时了,我不知道该怎么做.
鉴于此对象:
lst socials = {
foo: 'http://foo'
}
Run Code Online (Sandbox Code Playgroud)
我想在JSX中循环它.这有效:
let socialLinks = []
let socialBar
for (let social in socials) {
socialLinks.push(<li>
<a alt={social} href={socials[social]}>{ social }</a>
</li>)
}
if (socialLinks) {
socialBar = <div className='align-bottom text-center'>
<ul className='list-inline social-list mb24'>
{socialLinks}
</ul>
</div>
}
Run Code Online (Sandbox Code Playgroud)
但这不是(社会未定义):
let socialBar
if (socials) {
socialBar = <div className='align-bottom text-center'>
<ul className='list-inline social-list mb24'>
for(let social in socials)
{<li>
<a alt={social} href={socials[social]}>{ social }</a> // social is undefined
</li>}
</ul>
</div>
}
Run Code Online (Sandbox Code Playgroud)
social在第二个例子中未定义的原因是什么?我假设内部括号存在范围问题,但我没有成功修复它. …
我觉得这是一个应该回答却找不到答案的问题。
我已将 TS 项目切换到 ESM。所以我package.json现在有"type": "module"和我的tsconfig.json:
"target": "es2020",
"lib": ["es2020"],
"module": "node16",
"moduleResolution": "Node16",
"esModuleInterop": true, // Eases ESM support
"types": ["node"],
"allowSyntheticDefaultImports": true,
...
Run Code Online (Sandbox Code Playgroud)
大多数情况下,一切都很好,但有一些模块给我带来了麻烦。例如aedes和mqemitter并等待
import wait from 'wait'
await wait(1000)
Run Code Online (Sandbox Code Playgroud)
上面的代码“有效”,但 VSCode 红色强调了第二行中的“等待”错误:
This expression is not callable.
Type 'typeof import(".../node_modules/wait/wait")' has no call signatures.
Run Code Online (Sandbox Code Playgroud)
我当然尝试过:
import * as wait from 'wait'
Run Code Online (Sandbox Code Playgroud)
和
import { default as wait } from 'wait'
Run Code Online (Sandbox Code Playgroud)
我的 TS 配置中有我认为相关的 ESM 设置。
我使用的是 …
所以我试图解码以前在Node中使用php进行urlencoded的字符串.大约一个月前,我有它的工作:
querystring.unescape(str.replace(/\+/g, '%20'));
Run Code Online (Sandbox Code Playgroud)
然后它就停止了工作 - 不确定它是否是某些节点升级或什么.玩完之后,似乎我可以使用'unescape()',但我不确定它是否是万无一失的.
unescape(str.replace(/\+/g, '%20'));
Run Code Online (Sandbox Code Playgroud)
我的问题是什么是最好的方式,并有其他人注意到这个问题.请注意,第一行使用简单的字符串,但使用奇数字符分解 - 所以可能是一些我没有看到的编码问题.
这是一个字符串:
%E6%82%CCI-T%8C%01 + A
现在转到http://www.tareeinternet.com/scripts/unescape.html并对其进行解码.这是我原来的(它是RC4加密的字符串).我希望Node返回该字符串.
我在AngularJS应用程序中使用Restangular.我有一个表,每个项目都有一个删除链接.我想删除该项目并自动删除该行.但事情是它只从DB中删除.我如何重构事物以便DOM自动更新?
// The controller
angular.module('myApp').controller('ManageCtrl', function($scope, Restangular) {
$scope.delete = function(e) {
Restangular.one('product', e).remove();
};
Restangular.all('products').getList({}).then(function(data) {
$scope.products = data.products;
$scope.noOfPages = data.pages;
});
});
// The view
<li ng-repeat="product in products">
<a href="#" ng-click="delete(sheet._id)"></a>
</li>
Run Code Online (Sandbox Code Playgroud)
我也很想找到一个这样的例子 - 即使使用Angular资源.所有管理/数据表演示似乎都来自静态数据.
我看到许多测试描述,例如在Mocha中使用方法前面的#符号.这个的目的和起源是什么?我多年来一直这样做,就像一个旅鼠,所以我觉得是时候问为什么了:)
describe('#myMethod()', function() { ... })
Run Code Online (Sandbox Code Playgroud) 我正潜入Koa2,我看到了koa-compose.我知道我给它中间件它返回一个,但为什么呢?将多个中间件包装为一个而不是单独添加它们有什么好处?
app.use(compose(m1, m2))
Run Code Online (Sandbox Code Playgroud)
VS:
app.use(m1)
app.use(m2)
Run Code Online (Sandbox Code Playgroud) javascript ×4
node.js ×4
angularjs ×2
express ×2
es6-modules ×1
jsx ×1
koa ×1
mongodb ×1
mongoose ×1
nodes ×1
reactjs ×1
restangular ×1
typescript ×1
unit-testing ×1
urldecode ×1
validation ×1