যার কারও সাথে সমস্যা রয়েছে populateএবং তিনি এটি করতে চান:
- সাধারণ পাঠ্য এবং দ্রুত জবাব (বুদবুদ) এর সাথে চ্যাট করুন
- চ্যাটের জন্য 4 ডাটাবেসের সংগ্রহগুলি:
clients, users, rooms, messasges।
- 3 ধরণের প্রেরকের জন্য একই বার্তা ডিবি কাঠামো: বট, ব্যবহারকারী এবং ক্লায়েন্ট
refPathবা গতিশীল রেফারেন্স
populateসঙ্গে pathএবং modelঅপশন
- ব্যবহার
findOneAndReplace/ replaceOneসাথে$exists
- ফেচ করা নথি উপস্থিত না থাকলে একটি নতুন দস্তাবেজ তৈরি করুন
প্রেক্ষাপট
লক্ষ্য
- ডাটাবেসে একটি নতুন সাধারণ পাঠ্য বার্তা সংরক্ষণ করুন এবং এটি ব্যবহারকারী বা ক্লায়েন্টের ডেটা (2 টি ভিন্ন মডেল) দিয়ে পপুলেট করুন।
- ডেটাবেজে একটি নতুন কুইক রিপ্লাইস বার্তা সংরক্ষণ করুন এবং এটি ব্যবহারকারী বা ক্লায়েন্টের ডেটা দিয়ে পপুলেট করুন।
- প্রত্যেক বার্তার সংরক্ষণ তার প্রেরক টাইপ:
clients, usersও bot।
- প্রেরক
clientsবা usersতার মঙ্গুজ মডেলগুলি সহ কেবলমাত্র সেই বার্তাগুলিই তৈরি করুন । _সেন্ডার ধরণের ক্লায়েন্ট মডেলগুলি হ'ল clients, ব্যবহারকারী users।
বার্তা স্কিমা :
const messageSchema = new Schema({
room: {
type: Schema.Types.ObjectId,
ref: 'rooms',
required: [true, `Room's id`]
},
sender: {
_id: { type: Schema.Types.Mixed },
type: {
type: String,
enum: ['clients', 'users', 'bot'],
required: [true, 'Only 3 options: clients, users or bot.']
}
},
timetoken: {
type: String,
required: [true, 'It has to be a Nanosecond-precision UTC string']
},
data: {
lang: String,
// Format samples on https://docs.chatfuel.com/api/json-api/json-api
type: {
text: String,
quickReplies: [
{
text: String,
// Blocks' ids.
goToBlocks: [String]
}
]
}
}
mongoose.model('messages', messageSchema);
সমাধান
আমার সার্ভার সাইড এপিআই অনুরোধ
আমার কোড
chatUtils.jsআপনি যে বার্তাটি সংরক্ষণ করতে চান তার প্রকার পেতে ইউটিলিটি ফাংশন ( ফাইলটিতে):
/**
* We filter what type of message is.
*
* @param {Object} message
* @returns {string} The type of message.
*/
const getMessageType = message => {
const { type } = message.data;
const text = 'text',
quickReplies = 'quickReplies';
if (type.hasOwnProperty(text)) return text;
else if (type.hasOwnProperty(quickReplies)) return quickReplies;
};
/**
* Get the Mongoose's Model of the message's sender. We use
* the sender type to find the Model.
*
* @param {Object} message - The message contains the sender type.
*/
const getSenderModel = message => {
switch (message.sender.type) {
case 'clients':
return 'clients';
case 'users':
return 'users';
default:
return null;
}
};
module.exports = {
getMessageType,
getSenderModel
};
বার্তাটি সংরক্ষণের অনুরোধটি পাওয়ার জন্য আমার সার্ভার সাইড (নোডেজ ব্যবহার করে):
app.post('/api/rooms/:roomId/messages/new', async (req, res) => {
const { roomId } = req.params;
const { sender, timetoken, data } = req.body;
const { uuid, state } = sender;
const { type } = state;
const { lang } = data;
// For more info about message structure, look up Message Schema.
let message = {
room: new ObjectId(roomId),
sender: {
_id: type === 'bot' ? null : new ObjectId(uuid),
type
},
timetoken,
data: {
lang,
type: {}
}
};
// ==========================================
// CONVERT THE MESSAGE
// ==========================================
// Convert the request to be able to save on the database.
switch (getMessageType(req.body)) {
case 'text':
message.data.type.text = data.type.text;
break;
case 'quickReplies':
// Save every quick reply from quickReplies[].
message.data.type.quickReplies = _.map(
data.type.quickReplies,
quickReply => {
const { text, goToBlocks } = quickReply;
return {
text,
goToBlocks
};
}
);
break;
default:
break;
}
// ==========================================
// SAVE THE MESSAGE
// ==========================================
/**
* We save the message on 2 ways:
* - we replace the message type `quickReplies` (if it already exists on database) with the new one.
* - else, we save the new message.
*/
try {
const options = {
// If the quickRepy message is found, we replace the whole document.
overwrite: true,
// If the quickRepy message isn't found, we create it.
upsert: true,
// Update validators validate the update operation against the model's schema.
runValidators: true,
// Return the document already updated.
new: true
};
Message.findOneAndUpdate(
{ room: roomId, 'data.type.quickReplies': { $exists: true } },
message,
options,
async (err, newMessage) => {
if (err) {
throw Error(err);
}
// Populate the new message already saved on the database.
Message.populate(
newMessage,
{
path: 'sender._id',
model: getSenderModel(newMessage)
},
(err, populatedMessage) => {
if (err) {
throw Error(err);
}
res.send(populatedMessage);
}
);
}
);
} catch (err) {
logger.error(
`#API Error on saving a new message on the database of roomId=${roomId}. ${err}`,
{ message: req.body }
);
// Bad Request
res.status(400).send(false);
}
});
টিআইপি :
ডাটাবেসের জন্য:
- প্রতিটি বার্তা নিজেই একটি নথি।
- ব্যবহার করার পরিবর্তে
refPath, আমরা util ব্যবহার getSenderModelযে ব্যবহার করা হয় populate()। এটি বটের কারণে। এটি sender.typeহতে পারে: usersতার ডাটাবেস clientsসহ, তার ডাটাবেস সহ এবং botকোনও ডাটাবেস ছাড়াই। refPathসত্য মডেল রেফারেন্স প্রয়োজন, যদি না, Mongooose একটি ত্রুটি নিক্ষেপ করা।
sender._idObjectIdব্যবহারকারী এবং ক্লায়েন্ট, বা nullবট জন্য টাইপ হতে পারে ।
এপিআই অনুরোধ যুক্তি জন্য:
- আমরা
quickReplyবার্তাটি প্রতিস্থাপন করি (বার্তা ডিবিতে কেবলমাত্র একটি কুইকপ্লাই থাকতে হবে তবে আপনি যতটা সহজ পাঠ্য বার্তা চান) want আমরা এর findOneAndUpdateপরিবর্তে replaceOneবা ব্যবহার করি findOneAndReplace।
- আমরা কোয়েরি অপারেশন (ও
findOneAndUpdate) এবং প্রতিটিটির populateসাথে ক্রিয়াকলাপটি কার্যকর করি callback। এটি গুরুত্বপূর্ণ আপনি যদি ব্যবহার জানি না async/await, then(), exec()বা callback(err, document)। আরও তথ্যের জন্য পপুলেট ডকটি দেখুন ।
- আমরা
overwriteবিকল্প উত্তর ছাড়াই দ্রুত উত্তর বার্তাটি প্রতিস্থাপন করি$set কোয়েরি অপারেটর ।
- যদি আমরা দ্রুত উত্তরটি খুঁজে না পাই তবে আমরা একটি নতুন তৈরি করি। আপনার সাথে এটি মঙ্গুজকে বলতে হবে
upsertবিকল্পটির ।
- প্রতিস্থাপিত বার্তা বা নতুন সংরক্ষিত বার্তার জন্য আমরা কেবলমাত্র একটি সময় পপুলেট করি।
- আমরা কলব্যাকগুলিতে ফিরে আসি, এর সাথে
findOneAndUpdateএবং এর জন্য আমরা যে বার্তাটি সংরক্ষণ করেছি তা যাই হোক না কেন populate()।
- ইন
populate, আমরা এর সাথে একটি কাস্টম গতিশীল মডেল রেফারেন্স তৈরি করি getSenderModel। আমরা মঙ্গুজ গতিশীল রেফারেন্স ব্যবহার করতে পারি কারণ এর sender.typeজন্য botকোনও মঙ্গুজ মডেল নেই। আমরা একটি ব্যবহার ডাটাবেস জুড়ে পূর্ণ সঙ্গে modelএবং pathoptins।
আমি এখানে এবং সেখানে সামান্য সমস্যাগুলি সমাধান করার জন্য প্রচুর ঘন্টা ব্যয় করেছি এবং আমি আশা করি এটি কারও সাহায্য করবে! 😃