我在 MongoDB 中創建了第一個 Schema 和另一個(2 個表)來容納 2 個單獨的資訊。現在第一個作業正常,沒有挑戰,但第二個模式應該容納用戶資訊。
現在我在獲取用戶資訊時遇到了問題。我似乎不明白問題是什么。
架構看起來像這樣
var db = require('../database');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var SubscriptionSchema = new Schema({
company_name : String,
company_address : String,
company_city : String,
company_state : String,
companyrep_name : String,
companyrep_email : String,
company_telnum : String,
company_zip : String,
company_website : String,
timezone : String,
company_logo : String,
company_country : String,
product_requested : String,
methodof_payment : String,
dateof_request : String,
dateof_expiry : String,
});
var endUserRegisterSchema = new Schema({
username : String,
company_name : String,
password : String,
fullname : String,
company_ccy: String,
company_timezone : String
})
module.exports = mongoose.model('Subscription',SubscriptionSchema);
module.exports = mongoose.model('Users',endUserRegisterSchema);
然后將它添加到路由它應該是這樣的
users.js中應該保存資訊的路由器看起來像這樣
router.post('/', function (req, res) {
var newReg = new Users();
newReg.username = req.body.username;
newReg.company_name = req.body.company_name;
newReg.password = req.body.password;
newReg.fullname = req.body.fullname;
newReg.save(function(err,Users){
if(err){
res.send('Error registering User');
}else{
res.send(Users);
}
});
});
然后在app.js 上我添加了相應的 URL
瀏覽 REST api。所有這些都有效,但我有一個問題,它沒有將資訊完全保存到 Mongo DB。當我像這樣作為 JSON 傳遞時
{
"username":"admin@********.com",
"company_name":"blah blah blah",
"password":"supermna1",
"fullname":"Admin_blah blah"
}
我將其作為 Response 回傳,而不是完整資料
{
"_id": "619ddde9ff437222b17e888d",
"company_name": "blah blah blah",
"__v": 0
}
有什么我沒有做對的嗎?我在這里需要某種形式的澄清
uj5u.com熱心網友回復:
分離成更小的部分對我有用,現在一切都很好。無論創建什么模式,都將其拆分為單獨的模式。這就是我所做的,一切都很好。
uj5u.com熱心網友回復:
嘗試這樣做
router.post('/', async function(req, res) {
try {
var newReg = new Users();
newReg.username = req.body.username;
newReg.company_name = req.body.company_name;
newReg.password = req.body.password;
newReg.fullname = req.body.fullname;
await newReg.save();
res.send(newReg);
} catch (err) {
res.send('Error registering User');
}
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/364657.html
上一篇:當沒有條目時,awk無法正常作業,按過去15分鐘過濾訪問日志
下一篇:Cors錯誤strict-origin-when-cross-originsimplenodeJS-reactJS專案
