我的节点应用程序在本地运行正常,但在部署到Heroku时遇到错误.该应用程序使用Sequelize在一个/models文件夹,其中包含index.js,Company.js和Users.js.在本地,我可以使用以下代码导入模型/models/index.js:
// load models
var models = [
'Company',
'User'
];
models.forEach(function(model) {
module.exports[model] = sequelize.import(__dirname + '/' + model);
});
Run Code Online (Sandbox Code Playgroud)
这工作正常,但是,当我部署到Heroku时,应用程序崩溃时出现以下错误:
Error: Cannot find module '/app/models/Company'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at module.exports.Sequelize.import (/app/node_modules/sequelize/lib/sequelize.js:219:24)
at module.exports.sequelize (/app/models/index.js:60:43)
at Array.forEach (native)
at Object.<anonymous> (/app/models/index.js:59:8)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
Process exited with status 8
Run Code Online (Sandbox Code Playgroud)
最初我认为这是由于区分大小写(本地mac与heroku linux),但我移动了文件,进行了git提交,然后又移回并再次提交以确保Company.js在git存储库中大写.这并没有解决问题,我不确定问题是什么.
有人可以帮我理解如何在Sequelize中使用实例方法吗?我已经查看了文档,但发现它很稀疏.目前,我正在尝试在我的用户模型上使用setPassword和verifyPassword实例方法.当我尝试在REPL中调用代码时,在导入用户模型并同步数据库之后,我得到以下内容:
> models.User.setPassword('test');
TypeError: Object [object Object] has no method 'setPassword'
Run Code Online (Sandbox Code Playgroud)
以下是用户模型的代码:
var bcrypt = require('bcrypt');
module.exports = function(sequelize, DataTypes) {
return sequelize.define('User', {
email: { type: DataTypes.STRING, unique: true, allowNull: false, validate: { isEmail: true } },
password: { type: DataTypes.STRING, allowNull: false},
firstName: {type: DataTypes.STRING},
lastName: {type: DataTypes.STRING},
companyName: {type: DataTypes.STRING},
admin: {type: DataTypes.BOOLEAN, allowNull: false, defaultValue: false,},
forgotUrl: {type: DataTypes.STRING, unique: true},
forgotDate: {type: DataTypes.STRING},
lastLogin: {
type: DataTypes.DATE,
defaultValue: DataTypes.NOW
}
}, {
paranoid: true,
instanceMethods: …Run Code Online (Sandbox Code Playgroud) 我正在尝试在/register表单上提交POST后立即进行身份验证并登录用户.理想情况下,我希望用户能够注册,然后立即重定向到仪表板,而无需再次输入凭据.
我的服务器使用Passport 0.1.17,本地策略配置为使用电子邮件地址和密码登录.目前的代码是:
app.post('/register', function(req, res) {
// attach POST to new User variable
var registerUser = new User({ email: req.body.email, password: req.body.password, name: req.body.name });
// save registerUser Mongo
registerUser.save(function(err) {
if(err) {
console.log(err);
} else {
console.log('registerUser: ' + registerUser.email + " saved.");
}
});
// here is where I am trying to authenticate and then redirect
passport.authenticate('local', { failureRedirect: '/login', failureFlash: true }),
res.redirect('/dashboard');
});
Run Code Online (Sandbox Code Playgroud)
我如何重构此代码以保存新用户,然后进行身份验证并最终重定向到仪表板?
提前致谢!
[Error: data and hash arguments required]尝试在我的节点应用程序中验证用户的现有密码时出现错误.上下文是我要求我的用户在用户个人资料页面中更改之前验证他们的现有密码.我的堆栈是node + mondodb(通过mongoose)使用passport-local和bcrypt.
相关代码是:
// code trying to match that returns the aforementioned error
req.user.comparePassword(req.body.password, function (err, isMatch) {
if (err) {
return console.error(err);
}
if (isMatch) {
console.log('passwords match');
// now save new password
// Password verification
userSchema.methods.comparePassword = function (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, function (err, isMatch) {
if (err) return cb(err);
cb(null, isMatch);
});
};
}
}
Run Code Online (Sandbox Code Playgroud)
req.user引用当前用户对象,`req.body.password'是从用户的POST获得的密码.我在这里使用护照本地示例中的UserSchema,护照策略和Bcrypt配置.
有人可以提供有关如何在更新之前验证密码是否匹配的指导?
如何使用可添加逗号和货币格式的类似方法格式化<td>具有该类的所有表元素?如果与性能相关,则该表永远不会超过200行。这是当前的表格主体格式:class="contribution".toLocaleString();
<tbody>
<tr>
<td>2009-01-31</td>
<td class="contribution">-100000.00</td>
<td class="distribution">0.00</td>
<td class="nav">0.00</td>
</tr>
<tr>
<td>2013-05-15</td>
<td class="contribution">0.00</td>
<td class="distribution">56000.00</td>
<td class="nav">64000.00</td>
</tr>
</tbody>
Run Code Online (Sandbox Code Playgroud)
我当前正在使用的javascript似乎不起作用,它是:
$(document).ready( function() {
$("td.contribution").toLocaleString('en-US');
})
Run Code Online (Sandbox Code Playgroud)
如果有帮助,该网页确实加载了jquery 3。
我试图解组包含在具有相同结构的节点中的多个项目以进行进一步处理,但似乎无法访问数据,我不知道为什么.XML数据的结构如下(我试图访问所有Item的:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<datainfo>
<origin>NOAA/NOS/CO-OPS</origin>
<producttype> Annual Tide Prediction </producttype>
<IntervalType>High/Low Tide Predictions</IntervalType>
<data>
<item>
<date>2015/12/31</date>
<day>Thu</day>
<time>03:21 AM</time>
<predictions_in_ft>5.3</predictions_in_ft>
<predictions_in_cm>162</predictions_in_cm>
<highlow>H</highlow>
</item>
<item>
<date>2015/12/31</date>
<day>Thu</day>
<time>09:24 AM</time>
<predictions_in_ft>2.4</predictions_in_ft>
<predictions_in_cm>73</predictions_in_cm>
<highlow>L</highlow>
</item>
</data>
</datainfo>
Run Code Online (Sandbox Code Playgroud)
我的代码是:
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"os"
)
// TideData stores a series of tide predictions
type TideData struct {
Tides []Tide `xml:"data>item"`
}
// Tide stores a single tide prediction
type Tide struct {
Date string `xml:"date"`
Day string …Run Code Online (Sandbox Code Playgroud) node.js ×4
javascript ×3
passport.js ×2
sequelize.js ×2
bcrypt ×1
express ×1
git ×1
go ×1
heroku ×1
jquery ×1
mongoose ×1
postgresql ×1
xml ×1