Node.js 如何使用mongoose express和nodejs向mongodb添加新行
我有一个应用程序是使用本教程中建议的身份验证方法构建的: 它正在使用mongoose、mongodb和express,它的工作非常好 我现在尝试将条带支付与it整合。我正在尝试创建一个新的Paymentcard模型,就像教程创建用户模型一样,并在成功创建新客户时添加该卡 这是我的模型:Node.js 如何使用mongoose express和nodejs向mongodb添加新行,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我有一个应用程序是使用本教程中建议的身份验证方法构建的: 它正在使用mongoose、mongodb和express,它的工作非常好 我现在尝试将条带支付与it整合。我正在尝试创建一个新的Paymentcard模型,就像教程创建用户模型一样,并在成功创建新客户时添加该卡 这是我的模型: // app/models/paymentcard.js // load the things we need var mongoose = require('mongoose'); // define th
// app/models/paymentcard.js
// load the things we need
var mongoose = require('mongoose');
// define the schema for our user model
var paymentcardSchema = mongoose.Schema({
card : {
stripe_customer_id: String,
user_id: String,
id: String,
last4: String,
brand: String,
funding: String,
exp_month: String,
exp_year: String,
fingerprint: String,
country: String
}
});
// create the model for users and expose it to our app
module.exports = mongoose.model('Paymentcard', paymentcardSchema);
然后,我尝试在routes.js文件中使用以下内容添加新的支付卡:
stripe.customers.listCards(id, function(err, cards) {
// asynchronously called
var newpaymentcard = new Paymentcard();
newpaymentcard.save(function(err) {
if (err)
throw err;
return 'done';
});
});
但是,在日志中,当执行该代码位时,我会收到此错误:
/Users/adamcooke/Dropbox/node/authenticateexample/app/routes.js:259
var newpaymentcard = new Paymentcard();
^
ReferenceError: Paymentcard is not defined
at /Users/adamcooke/Dropbox/node/authenticateexample/app/routes.js:259:56
at null._onTimeout (/Users/adamcooke/Dropbox/node/authenticateexample/node_modules/stripe/lib/StripeResource.js:78:34)
at Timer.listOnTimeout [as ontimeout] (timers.js:112:15)
npm ERR! Darwin 14.0.0
npm ERR! argv "node" "/usr/local/bin/npm" "start"
npm ERR! node v0.10.34
npm ERR! npm v2.1.16
npm ERR! code ELIFECYCLE
npm ERR! nodetest2@0.0.0 start: `node server.js`
npm ERR! Exit status 8
你知道我遗漏了什么吗?我认为这与我定义paymentcard模型的方式有关,或者我没有在routes.js中提供它
如果您需要更好地查看,我也在github上提供了代码:
passport.js中的Paymentcard变量是该文件的私有文件,据我所知,它没有被使用。您需要将同一行添加到routes.js,以便该文件中也可以使用该行
var Paymentcard = require('../app/models/paymentcard');
我看不出你在哪里使用require来实现Paymentcard模式。这是在哪里完成的?我的理解是,这是在配置文件夹中的passport.js文件中完成的,Paymentcard var是passport.js文件的私有文件,据我所知没有使用;你需要在routes.js.hmm中添加相同的内容,就是这样!谢谢把这么多东西放在routes.js中是可以接受的,还是真的不重要?通常最好把你的路线处理功能放在单独的模块中,然后只拉入它们需要的模型。