#node.js #mongoose
#node.js #мангуст
Вопрос:
Я пытаюсь воспроизвести этот проект push-уведомлений, написанный на TS, но с использованием javascript.
Это следующая функция:
Subscription Model:
const mongoose = require('mongoose');
const Schema = mongoose.Schema
const SubscriptionModel = new Schema({
endpoint: { type: String, unique: true, required: true },
expirationTime: { type: Number, required: false },
keys: {
auth: String,
p256dh: String,
},
});
const model = mongoose.model('Subscription', SubscriptionModel);
module.export = model
SubscriptionRepository
const Subscription = require('../model/SubscriptionModel')
const create = async (subscription) => {
try{
const newSubscription = new Subscription.model(subscription);
const savedSubscription = await newSubscription.save();
return savedSubscription.toObject();
}catch(err){
return err
}
};
module.exports = {create:create}
Подписка на маршрут
app.post("/subscription", async (req, res) => {
try{
(async() => {
const subscription = req.body;
const newSubscription = subscriptionRepository.create(subscription);
console.log(newSubscription);
res.status(201).json(newSubscription)
})();
}catch(err){
console.log(err);
}
});
Ошибка:
Обещание { TypeError: Subscription.model не является конструктором }
Это мой первый раз, когда я использую Moongose, я понятия не имею, даже ищу, как исправить! Любое предложение, чтобы помочь мне?
Вложенный объект:
{ endpoint:
'https://fcm.googleapis.com/fcm/send/dYGGHvDYLtk:APA91bHIkbLj-Fi0VKrhRuemQZiwc1G49bls0obRGlyPJ6MLisgm3s_lGLvts9jKUaAHKWxxMFlw7imLY8YqEU1C83YNRREdZ70nnVamFFLSANWpZ922z-tysSQhEjBquhISE9rv2RtK',
expirationTime: null,
keys:
{ p256dh:
'BOVN3GYiheBK77iPxZecbagZCMXLi06YE8pe-O-dKSmd8r_ccObMYyJ8oYbIQw7htNSxIv4mP5awABRq0LskE6I',
auth: '8x9bTem324oY4-P9jKcuSQ' } }
Вывод ошибки:
Promise {
TypeError: Subscription.model is not a constructor
at Object.create (/home/thiago/Documentos/Coding/push_notification/controller/subscriptionRepository.js:5:33)
at /home/thiago/Documentos/Coding/push_notification/server.js:31:54
at app.post (/home/thiago/Documentos/Coding/push_notification/server.js:34:7)
at Layer.handle [as handle_request] (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/layer.js:95:5)
at next (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/route.js:137:13)
at Route.dispatch (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/layer.js:95:5)
at /home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:281:22
at Function.process_params (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:335:12)
at next (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:275:10)
at serveStatic (/home/thiago/Documentos/Coding/push_notification/node_modules/serve-static/index.js:75:16)
at Layer.handle [as handle_request] (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:317:13)
at /home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:335:12)
at next (/home/thiago/Documentos/Coding/push_notification/node_modules/express/lib/router/index.js:275:10) }
Комментарии:
1. Вы пробовали
const newSubscription = new Subscription(subscription)
?2. Только что попробовал! Некоторая ошибка ` TypeError: Subscription.model не является конструктором`
3. выведите на консоль весь
Subscription
объект и добавьте этот вывод в свой пост4. обновленный вопрос
5. Это похоже на объект записи, а не на саму модель. Показать вывод
model
передmodule.export = model