我最近开始学习如何使用Sails.js,我遇到了一个小问题.
我有一个名为申请人的模型,如下:
module.exports = {
attributes: {
name: {
type: 'STRING',
required: true
},
email: {
type: 'STRING',
required: true
}
}
};
Run Code Online (Sandbox Code Playgroud)
我构建了索引操作以返回Applicant模型的所有实例:
module.exports = {
index: function(req, res) {
Applicant.findAll().done(function(err, applicants) {
res.view({
apps: applicants
});
});
}
};
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的观点:
<section id="applicants">
<h1>List of applicants</h1>
<ul>
<% for (applicant in apps) { %>
<li><%= applicant.name %> <%= applicant.email %></li>
<% } %>
</ul>
</section>
Run Code Online (Sandbox Code Playgroud)
但是,当我加载相应的URL时,我看不到我的数据显示.
我已经阅读了Sails.js文档,但我似乎无法找到我的代码有什么问题.
如果有人可以帮助我解决这个问题,那就太棒了.
谢谢!
我在视图上有3个表单,在提交时,在mysql数据库中添加新条目.我想在添加条目时发送一条警告说"你已成功添加X",而不从页面导航.
// Form to be Submitted
<form method="post" action="route/action/">
<input type="text" name="name">
</form>
// Route
exports.action = function (req, res) {
client.query();
// What kind of response to send?
}
Run Code Online (Sandbox Code Playgroud)
我该如何发送提醒?我应该发送什么样的回复?
谢谢!
在过去的几天里,我一直在使用Passport.js开发我的第一个用户登录和身份验证系统.很尴尬,我已经完成了它,它的工作原理与预期一致.问题是,即使我已经阅读了大量文章并在线查看了数十个示例,但我似乎并不完全理解代码本身.我没有理解它背后的过程以及它为什么会这样发生的问题.如果你能为我澄清代码的某些部分,我将非常感激.这是工作代码,存储在我的app.js文件中:
// Passport session setup
passport.serializeUser(function (user, done) {
done(null, user._id);
});
passport.deserializeUser(function (id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// Use the Local Strategy within passport
passport.use(new LocalStrategy(function (username, password, done) {
User.findOne({ username: username }, function(err, user) {
if (err) {
return done(err);
}
if (!user) {
return done(null, false, { message: 'Unknown user: ' + username});
}
user.comparePassword(password, function(err, isMatch) {
if (err) {
return done(err);
}
if (isMatch) {
return done(null, user); …Run Code Online (Sandbox Code Playgroud) 在过去一小时左右的时间里,我一直试图弄清楚为什么在启动Express 4.0应用程序时出现以下错误:
Error: Cannot find module 'html'
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 new View (e:\Multivision\node_modules\express\lib\view.js:43:49)
at Function.app.render (e:\Multivision\node_modules\express\lib\application.
js:499:12)
at ServerResponse.res.render (e:\Multivision\node_modules\express\lib\respon
se.js:955:7)
at e:\Multivision\server.js:11:6
at Layer.handle [as handle_request] (e:\Multivision\node_modules\express\lib
\router\layer.js:76:5)
at next (e:\Multivision\node_modules\express\lib\router\route.js:100:13)
Run Code Online (Sandbox Code Playgroud)
基本上,我只想在我的视图中使用普通的html文件,因为我正在尝试使用MEAN堆栈.您可以在下面找到app.js我尝试运行的两个不同版本的代码:
版本1
var express = require('express'),
app = express();
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development',
port = 3000;
app.set('views', __dirname + '/server/views');
app.set('view engine', 'html');
app.get('*', function(req, res) {
res.render('index');
});
app.listen(port);
console.log('Listening on …Run Code Online (Sandbox Code Playgroud) 我试图从mongodb shell查询我的数据库以检索所有条目.不过,我的find()方法没有返回任何内容.
这是我正在使用的猫鼬模型:
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
var ArticleSchema = new Schema({
title: String,
content: String,
author: { type: String, default: 'Vlad'},
postDate: { type: Date, default: Date.now }
});
ArticleSchema.methods.formatTitle = function() {
var link = this.title.replace(/\s/g, '-');
return '/article/' + link;
};
ArticleSchema.methods.snapshot = function() {
var snapshot = this.content.substring(0, 500);
return snapshot;
};
module.exports = mongoose.model('Article', ArticleSchema);
Run Code Online (Sandbox Code Playgroud)
在浏览器中加载我的Express.js应用程序时,我没有显示使用我的API添加的所有文章的问题.不过,我只想进入mongo shell并查询它们.我使用了以下查询:
// blog is the database name (mongodb://localhost:27017/blog)
db.blog.find()
Run Code Online (Sandbox Code Playgroud)
我得到一个空字符串作为回报,基本上是控制台中的新行.
我正在使用Cloud 9 IDE使用Sails.js(node.js MVC框架)开发一个简单的CRUD应用程序.直到今天我都没有启动Sails.js服务器.
今天,我一直在尝试启动sails js服务器,但我不断收到此错误:
warn: error raised: Error: listen EACCES
error: Server doesn't seem to be starting.
error: Perhaps something else is already running on port 8080?
Run Code Online (Sandbox Code Playgroud)
我检查了我的/config/local.js文件,一切都很好,应该是这样.端口设置为process.env.PORT || 1337所以它不应该有任何问题解雇服务器.
我期待着你的见解.
谢谢!
我目前正在开发一个Sails.js项目,我想将一个文件上传到服务器并将链接保存到我的数据库中.
例如,当我单击添加文件时,它应该允许我从我的计算机中选择一个文件,当我单击提交时,它应该将文件上传到我指向的URL(如果它不存在则创建该文件夹).
这可以用Sails.js完成吗?如果有,怎么样?
谢谢!