有没有办法将created_at和updated_at字段添加到mongoose模式,而不必在每次调用新的MyModel()时都传递它们?
created_at字段将是一个日期,仅在创建文档时添加.每当在文档上调用save()时,updated_at字段将使用新日期进行更新.
我在我的架构中尝试了这个,但除非我明确地添加它,否则该字段不会显示:
var ItemSchema = new Schema({
name : { type: String, required: true, trim: true }
, created_at : { type: Date, required: true, default: Date.now }
});
Run Code Online (Sandbox Code Playgroud) 我想用bash将字符串中的第一个字符大写.
foo="bar";
//uppercase first character
echo $foo;
Run Code Online (Sandbox Code Playgroud)
应打印"Bar";
以下测试表现得很奇怪:
it('Should return the exchange rates for btc_ltc', function(done) {
var pair = 'btc_ltc';
shapeshift.getRate(pair)
.then(function(data){
expect(data.pair).to.equal(pair);
expect(data.rate).to.have.length(400);
done();
})
.catch(function(err){
//this should really be `.catch` for a failed request, but
//instead it looks like chai is picking this up when a test fails
done(err);
})
});
Run Code Online (Sandbox Code Playgroud)
我该如何妥善处理被拒绝的承诺(并对其进行测试)?
我该如何正确处理失败的测试(即:expect(data.rate).to.have.length(400);
?
这是我正在测试的实现:
var requestp = require('request-promise');
var shapeshift = module.exports = {};
var url = 'http://shapeshift.io';
shapeshift.getRate = function(pair){
return requestp({
url: url + '/rate/' + pair,
json: true
}); …
Run Code Online (Sandbox Code Playgroud) 我正在使用express + node.js并且我有一个req对象,浏览器中的请求是/ account但是当我记录req.path时我得到'/'---不是'/ account'.
//auth required or redirect
app.use('/account', function(req, res, next) {
console.log(req.path);
if ( !req.session.user ) {
res.redirect('/login?ref='+req.path);
} else {
next();
}
});
Run Code Online (Sandbox Code Playgroud)
req.path是/什么时候应该是/ account?
我正在尝试按顺序加载一组脚本,但onload事件并没有为我开火.
var scripts = [
'//cdnjs.cloudflare.com/ajax/libs/less.js/1.3.3/less.min.js',
'//cdnjs.cloudflare.com/ajax/libs/handlebars.js/1.0.0-rc.3/handlebars.min.js',
MK.host+'/templates/templates.js'
];
function loadScripts(scripts){
var script = scripts.shift();
var el = document.createElement('script');
el.src = script;
el.onload = function(script){
console.log(script + ' loaded!');
if (scripts.length) {
loadScripts(scripts);
}
else {
console.log('run app');
MK.init();
}
};
$body.append(el);
}
loadScripts(scripts);
Run Code Online (Sandbox Code Playgroud)
我猜想当使用jQuery将元素附加到DOM时,el.onload等本机事件不会触发.如果我使用原生,document.body.appendChild(el)
那么它会按预期激发.
我正在为item.comments列表添加评论.我需要在我在响应中输出之前获取comment.created_by用户数据.我该怎么做?
Item.findById(req.param('itemid'), function(err, item){
var comment = item.comments.create({
body: req.body.body
, created_by: logged_in_user
});
item.comments.push(comment);
item.save(function(err, item){
res.json({
status: 'success',
message: "You have commented on this item",
//how do i populate comment.created_by here???
comment: item.comments.id(comment._id)
});
}); //end item.save
}); //end item.find
Run Code Online (Sandbox Code Playgroud)
我需要在res.json输出中填充comment.created_by字段:
comment: item.comments.id(comment._id)
Run Code Online (Sandbox Code Playgroud)
comment.created_by是我的mongoose CommentSchema中的用户引用.它目前只给我一个用户ID,我需要填充所有用户数据,密码和盐字段除外.
以下是人们提出的架构:
var CommentSchema = new Schema({
body : { type: String, required: true }
, created_by : { type: Schema.ObjectId, ref: 'User', index: true }
, created_at : { type: Date }
, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试触发<input type="file">
元素的click事件button
.
<input id="upload"
type="file"
ng-file-select="onFileSelect($files)"
style="display: none;">
<button type="button"
ng-click="angular.element('#upload').trigger('click');">Upload</button>
Run Code Online (Sandbox Code Playgroud)
通常的做法是隐藏已知的uglified beast <input type=file>
并通过其他方式触发它的click事件.
我想创建一个除了默认的'foo'.titlecase之外的方法,它将正确地添加"占有性".
字符串是用户的名字(< - 只是在那里做了一个!)
例如:"sam"是用户<%= user.titlecase.possessive +'个人资料'%> => #Sam的个人资料
它只需要处理边缘情况,如:
钢人队的简介(应该是钢人队的简介)罗斯的简介(应该是罗斯的简介)
我正在尝试/login?ref=/some/path
登录后重定向到的参数:
const ref = $page.url.searchParams.get('ref') || '/dashboard';
Run Code Online (Sandbox Code Playgroud)
但是我收到这个错误:
TypeError: Cannot read properties of undefined (reading 'searchParams')