0

I want to store all users ID of users clicking on a button of my Messenger Bot. I'm not really familiar with Javascript and MongoDB. I tried this but it seems it's not working:

-Index.js:

// DB
var mongoose = require("mongoose");

var db = mongoose.connect(process.env.MONGODB_URI);
var User = require("./models/user");

-Model models/user.js

var mongoose = require("mongoose");
var Schema = mongoose.Schema;

var UserSchema = new Schema({
    _id: {type: String}
});

module.exports = mongoose.model("User", UserSchema);

-Index.Js to save the User ids using a payload:

if (text === '{"payload":"SUB_YES_PAYLOAD"}') {
                User.insert({ _id: event.sender.id })
        sendTextMessage(sender, "Merci pour votre inscription ! Voici les derniers articles publiés :", token)
                sendGenericMessage(sender)
        continue
      }

Thanks for your help!

nico_lrx
  • 715
  • 1
  • 19
  • 36

2 Answers2

0

Reading the documentation, you need create the model like this:

var User = mongoose.Schema({
    _id: String
});

and create new instance like this (I modify with you details):

var user = new User.create({ id: event.sender.id' });

Read more: http://mongoosejs.com/docs/

GIA
  • 1,400
  • 2
  • 21
  • 38
0

Basically there are two easy ways to do so.

First

User.create(data).then(function(user) {
  console.log(user);
});

Second

var userInstance = new User(data);
userInstance.save().then(function(user){
  console.log(user);
});

mongoose would be fun if used with bluebird

mongoose.Promise = require('bluebird'); View this documentation for more.

Lekhnath
  • 4,532
  • 6
  • 36
  • 62