如何为外键允许null?我有一个through和belongsToMany协会:
Shelf
belongsToMany(Book, { through: Library, as: "Books"});
Book
belongsToMany(Shelf, { through: Library, as: "Shelves"});
Library
section: DataTypes.STRING,
// ... other fields
Run Code Online (Sandbox Code Playgroud)
我想记录null的上bookId中Library:
Library.create({
section: "blah",
bookId: null,
shelfId: 3
});
Run Code Online (Sandbox Code Playgroud)
由于Sequelize自动创建bookId和shelfId连接表中Library,我将如何指定我希望允许null的bookId外键的Library?
我正在阅读文档,它提到了两种做upsert的方法:findOrCreate()或upsert().我阅读了描述,但我仍然不清楚它们的区别.
在upsert()接着说:
Note that the unique index must be defined in your sequelize model and not just in the table. Otherwise you may experience a unique constraint violation, because sequelize fails to identify the row that should be updated.
Run Code Online (Sandbox Code Playgroud)
这是否upsert()明确要求我id在模型中定义一个UNIQUE主要字段?即:
var Something = sequelize.define("Something", { id: {
type: DataTypes.INTEGER,
primaryKey: true,
unique: true,
allowNull: false}});
Run Code Online (Sandbox Code Playgroud)
如果是这样,为什么没有findOrCreate()这个限制?
有人可以解释何时findOrCreate()和upsert()应该使用的用例,为什么upsert()在findOrCreate()不需要它时有独特的约束要求?
默认情况下,如果在a中没有选择任何内容,则angular使用空值<select>.如何将其更改为文字Please select one?
我有一个表单,当提交后某些内容没有正确时,我希望能够向用户提供特定的错误消息.我使用Sequelize作为我的ORM,并且返回的承诺对所有嵌套代码都有点混乱.
例如,如果我们有两个要更新的模型:User和Photo:
models.User.find({where: {name: 'bob' }}).then(function(user) {
if(user) {
user.updateAttributes({email: email}).then(function(user) {
models.Photo.find({where: { hash: hash}}).then(function(photo) {
if(photo)
res.json({"message":"updated"});
else
res.status(404).json({"error": "Could not find Photo"});
}).catch(err) {
res.status(500).json({"error": err});
});
}).catch(function(err) {
res.status(500).json({"error": err});
});
} else {
res.status(404).json({"error": "Could not find user"});
}
}).catch(function(err) {
res.status(500).json({"error": err});
});
Run Code Online (Sandbox Code Playgroud)
如果我在表单中有10个字段要更新,那么所有嵌套代码都会变得霸道.
如果我希望有特定的错误描述,还有更易读的代码,可以给出什么建议?是否有可能在一个代码块中捕获所有404和500错误而不是像我一样分解它们?
我正在使用expressjs。我的问题是,如果由于某种原因我未提供响应,则会再次调用路由。在Firebug中,请求的状态将为“待处理”。最终,在第二次调用路由后,我得到了响应ERR_EMPTY_RESPONSE。
这是一个简单的测试:
// main.html
<html>
<body>
<span>test</span>
</body>
</html>
<script>
$.get("/test");
</script>
// app.js
var express = require('express');
var app = express();
app.use(express.static(__dirname + '/public'));
app.get('/test', function(req, res) {
console.log("I'm called");
});
http.listen(3000, function() {
console.log('Express started');
});
Run Code Online (Sandbox Code Playgroud)
您会I'm called在到达路线时看到,然后在大约15到20秒后再次看到。
看来Express或Node正在某种程度上导致路由再次运行。为什么会这样,如何预防呢?
编辑:做一个简单的测试,演示问题。
Nodemailer作者已明确表示他不支持承诺。我想我应该尝试使用 bluebird,但我的尝试似乎没有捕获 Nodemailer 抛出的任何错误:
var nodemailer = require('nodemailer');
var Promise = require('bluebird');
// build the transport with promises
var transport = Promise.promisifyAll( nodemailer.createTransport({...}) );
module.exports = {
doit = function() {
// Use bluebird Async
return transport.sendMailAsync({...});
}
}
Run Code Online (Sandbox Code Playgroud)
然后我通过这样做来调用它:
doit().then(function() {
console.log("success!");
}).catch(function(err) {
console.log("There has been an error");
});
Run Code Online (Sandbox Code Playgroud)
但是,当提供无效的电子邮件时,我看到以下内容:
var nodemailer = require('nodemailer');
var Promise = require('bluebird');
// build the transport with promises
var transport = Promise.promisifyAll( nodemailer.createTransport({...}) );
module.exports = {
doit = function() …Run Code Online (Sandbox Code Playgroud) Express和Angular都有自己的csrf中间件.我根本无法让他们工作,互联网上似乎没有任何关于此问题的连贯指南.我的理解是express 4.0使用csurf作为其csrf中间件,我必须设置X-XSRF-TOKENangularjs.
有关如何执行此操作的分散部分,有时会发生冲突的信息:
如何在node.js/express中测试受csrf保护的端点
但是我尝试了它们并且它们不起作用.我_csrf总是undefined在Angular客户端上,success尽管没有从客户端给出csrf令牌,但csurf总是给出.
此外,我express-jwt用来保持用户会话,所以我不确定这是否会干扰cookie-session(curf要求).
这是我用csrf处理注册的简单角度/快速应用程序(不工作):
angular app.js
var app = angular.module('app', ['ngCookies', 'ui.router']);
app.config(function($stateProvider) {
$stateProvider.state('register', {
url: '/register',
controller: 'RegisterCtrl',
templateUrl: 'views/register.html'
});
});
// register csrf
app.run(['$http', '$cookies', function($http, $cookies) {
$http.defaults.headers.post['X-XSRF-TOKEN'] = $cookies.csrftoken;
}]);
// controller
app.controller('RegisterCtrl', ['$scope', '$cookies', '$http',
function($scope, $cookies, $http) {
$scope.data = { email: "", password: "", _csrf: $cookies._csrf};
$scope.submitForm = function() {
// This will alert …Run Code Online (Sandbox Code Playgroud) 为什么以下只与完全匹配,而不是部分匹配?
body: {
query: {
filtered: {
filter: {
bool: {
should: [
{ query: { match: { "name": "*"+searchterm+"*" }}},
]
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
"*"+searchterm+"*"应匹配任何包含的单词searchterm.即
item1
item2
0item
Run Code Online (Sandbox Code Playgroud)
但它只匹配单词确切, searchterm即仅匹配item.为什么是这样?
node.js ×6
javascript ×4
express ×3
sequelize.js ×3
angularjs ×2
postgresql ×2
bluebird ×1
csrf ×1
ng-options ×1
nodemailer ×1
promise ×1
wildcard ×1