使用Mongoose将文档(记录)插入MongoDB有哪些不同的方法?
我目前的尝试:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var notificationsSchema = mongoose.Schema({
"datetime" : {
type: Date,
default: Date.now
},
"ownerId":{
type:String
},
"customerId" : {
type:String
},
"title" : {
type:String
},
"message" : {
type:String
}
});
var notifications = module.exports = mongoose.model('notifications', notificationsSchema);
module.exports.saveNotification = function(notificationObj, callback){
//notifications.insert(notificationObj); won't work
//notifications.save(notificationObj); won't work
notifications.create(notificationObj); //work but created duplicated document
}
Run Code Online (Sandbox Code Playgroud)
知道为什么插入和保存在我的情况下不起作用?我尝试创建,它插入2个文件而不是1.这很奇怪.
下面是一个简单的反击计数器.但我有3个困惑.
第3行的状态是什么?看起来像一个全局变量,它有没有意义,如果它有var或const之前.这是一个生命周期函数/ var吗?
我必须从反应中导入组件吗?我记得我不需要在第15节那样做.
prevState来自哪里?
Run Code Online (Sandbox Code Playgroud)import React, { Component } from 'react'; class Counter extends Component { state = { value: 0 }; increment = () => { this.setState(prevState => ({ value: prevState.value + 1 })); }; decrement = () => { this.setState(prevState => ({ value: prevState.value - 1 })); }; render() { return ( <div> {this.state.value} <button onClick={this.increment}>+</button> <button onClick={this.decrement}>-</button> </div> ) } }
在一个节点项目中,我发现了两种依赖:
"dependencies": {
"axios": "0.9.1",
"express": "4.13.4",
"lodash": "4.6.1",
"react": "^0.14.7",
"react-dom": "^0.14.7",
"react-redux": "^4.4.0",
"react-router": "^2.0.0",
"redux": "^3.3.1"
},
"devDependencies": {
"babel-core": "^6.5.2"
}
Run Code Online (Sandbox Code Playgroud)
我知道作者通过它安装它 npm install babel-core --save -dev
但这是为了什么?当您推送代码时,devDependencies模块仍然存在.
我正在使用ES6 babel做出反应,现在对于新版本的反应,反应DOM不再是它的一部分了.我对以下代码的疑问是,它是第一行需要的吗?因为我无处需要React,但最后一行我需要ReactDOM.
const React = require('react')
const ReactDOM = require('react-dom')
const App = () => {
return (
<div className='app-container'>
<div className='home-info'>
<h1 className='title'>sVideo</h1>
<input className='search' type='text' placeholder='Search' />
<button className='browse-all'> or Browse All</button>
</div>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('app'))
Run Code Online (Sandbox Code Playgroud) 我使用$gte和$lte误用其他的条件?我得到了空数组,但是当我这样做的时候,我user_email:req.bodu.user_email就能获得所有数据。
function firstDayOfMonth() {
var d = new Date(Date.apply(null, arguments));
d.setDate(1);
return d.toISOString();
}
function tomorrowDate() {
var d = new Date();
d.setDate(d.getDate() + 1)
return d.toISOString();
}
Users.find({
"user_email": req.body.user_email,
"createdAt": {
"$gte": firstDayOfMonth(),
"$lte": tomorrowDate()
}
},
function(err, response) {
if (!err) {
console.log(response)
res.json(response);
}
});
Run Code Online (Sandbox Code Playgroud) 我正在关注一个反应教程,这是作者为创建一个基本的React组件而给出的示例代码:
const React = require('react')
const ReactDOM = require('react-dom')
const App = () => {
return (
<div className='app-container'>
<h1>Hello</h1>
</div>
)
}
ReactDOM.render(<App />, document.getElementById('app'))
Run Code Online (Sandbox Code Playgroud)
他声称这是ES6.
但后来我看到了另一种创建组件的方法.
class App extends React.Component {
render(){
return <h1>Hello</h1>;
}
}
Run Code Online (Sandbox Code Playgroud)
嗯,我现在很困惑.有没有标准的做法做出反应?
我们经常定义这样的模式
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Account = new Schema({
username: String,
password: String
});
module.exports = mongoose.model('account', Account);
Run Code Online (Sandbox Code Playgroud)
我们必须传入与模式匹配的对象,否则没有任何效果。但说我想保存一些动态的东西,我什至不知道它们是什么,例如它可以是
{'name':'something',birthday:'1980-3-01'}
Run Code Online (Sandbox Code Playgroud)
或者只是其他任何东西
{'car':'ferrari','color':'red','qty':1}
Run Code Online (Sandbox Code Playgroud)
那么如何设置架构呢?
我按照教程使用react.js 发布了这个https://jsbin.com/foxoxejilu/1/edit?js,output.
我对道具和国家感到困惑.在save方法中Note component,这条线做了什么
this.props.onChange(this.refs.newText.value, this.props.id)
Run Code Online (Sandbox Code Playgroud)
而且我没有在代码中的其他地方看到onChange和onRemove函数,是一个预构建函数的反应吗?如何反应知道DOM被更改或删除?
我在下面使用formData,并传递结果。但是,如果用户不止一次重复数据,则会被复制。因此,我必须在添加任何值之前清除/重置formData。
我可以像f.name =''一样一对一地做,我有很多钥匙。有没有更好的方法来解决问题?
$('input').change(function(){
// Create a new formdata but at first have to clear/reset it
var fd = new FormData();
var f = $.canvasResize('dataURLtoBlob', data);
f.name = file.name;
fd.append($('#area input').attr('name'), f);
})
Run Code Online (Sandbox Code Playgroud) 我使用了连接多方中间件,我有以下代码:
console.log(req.files)
var data = {
Key: req.body.filename,
Body: req.files,
ContentType: 'image/jpeg'
};
s3Bucket.putObject(data,function(err,result){
console.log(err);
});
Run Code Online (Sandbox Code Playgroud)
我在终端中得到以下结果:
{ photo:
{ fieldName: 'photo',
originalFilename: 'blob',
path: '/var/folders/n1/fr69rt5j01sfwjhsh3jx3gh00000gn/T/Tw4pwoPQ5D7zCGxrk3U1ywKp',
headers:
{ 'content-disposition': 'form-data; name="photo"; filename="blob"',
'content-type': 'image/jpeg' },
size: 50138,
name: 'blob',
type: 'image/jpeg' } }
{ [InvalidParameterType: Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object]
message: 'Expected params.Body to be a string, Buffer, Stream, Blob, or typed array object',
code: 'InvalidParameterType',
Run Code Online (Sandbox Code Playgroud)
任何线索这不起作用?应该是什么Body值?
我的下面的代码可以很好地从用户在猫鼬中使用 $inc 扣除信用(它适用于增量,这很棒),但该值可能会变成负数,这是我不想要的,有什么选项可以阻止这种情况吗?
module.exports.deduct_credit = function(subscriber_email,callback){
Users.findOneAndUpdate(
{email: subscriber_email},
{$inc:{credit:price_per_use}},
{new: true})
.exec(callback);
}
Run Code Online (Sandbox Code Playgroud) 试图创造一个反应但失败的李.错误是在map()附近,我得到的错误我没有定义,为什么?
const TodoItems = React.creatClass({
getInitialState() {
return {
items : [
{id:1,name:"Gym"},
{id:2,name:"Jump"},
{id:3,name:"Racing"}
]
}
},
renderItem(){
return(
<ul>
this.state.items.map(item,i =>
<li key={i}>item.name</li>
)
</ul>
)
},
render(){
return (
<renderItem />
)
}
})
Run Code Online (Sandbox Code Playgroud)