Developing a Mini Program with Cloud Functions: Posting, Viewing, and Commenting

Using the cloud‑development model, the author built a mini‑program that lets users publish posts with optional images, browse a list of posts, view details, and add comments, leveraging cloud functions for CRUD operations, cloud database for storage, and cloud storage for images, while noting the rapid prototyping advantage and the modest 2 GB database and 5 GB storage limits.

Tencent Cloud Developer
Tencent Cloud Developer
Tencent Cloud Developer
Developing a Mini Program with Cloud Functions: Posting, Viewing, and Commenting

The author recently explored mini‑program development and built a simple app using the cloud‑development model, experiencing the three core capabilities: cloud functions, database, and storage.

Cloud development simplifies the authentication flow and weakens the need for a traditional backend, allowing a newcomer to create a functional prototype within a weekend.

Functional analysis : The mini‑program supports three basic features – publishing posts, browsing posts, and publishing comments. These are backed by cloud database (for posts and comments), cloud storage (for images), and cloud functions (for CRUD operations).

Publishing a post : If a post has no image, it is written directly to the database. If it includes images, each image must first be uploaded to cloud storage to obtain a fileId, which is then stored in the database. The core upload code is:

for (let i = 0; i < img_url.length; i++) {
  var str = img_url[i];
  var obj = str.lastIndexOf("/");
  var fileName = str.substr(obj + 1);
  console.log(fileName);
  wx.cloud.uploadFile({
    cloudPath: 'post_images/' + fileName, // must specify filename
    filePath: img_url[i], // temporary file path
    success: res => {
      console.log(res);
      img_url_ok.push(res.fileID);
      if (img_url_ok.length == img_url.length) {
        console.log(img_url_ok);
        that.publish(img_url_ok);
      }
    },
    fail: err => {
      console.log('fail: ' + err.errMsg);
    }
  })
}

When all images are uploaded (checked by img_url_ok.length == img_url.length), the app calls a cloud function publish_post to insert the post record:

publish: function(img_url_ok) {
  wx.cloud.init();
  wx.cloud.callFunction({
    name: 'publish_post',
    data: {
      openid: app.globalData.openId,
      author_name: app.globalData.userInfo.nickName,
      content: this.data.content,
      image_url: img_url_ok,
      publish_time: "",
      update_time: ""
    },
    success: function(res) {
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];
      prevPage.setData({ update: true });
      wx.hideLoading();
      wx.navigateBack({ delta: 1 });
    },
    fail: console.error
  })
}

The cloud function implementation (Node.js) adds a document to the post_collection collection:

exports.main = async (event, context) => {
  try {
    return await db.collection('post_collection').add({
      data: {
        author_id: event.userInfo.openId,
        author_name: event.author_name,
        content: event.content,
        image_url: event.image_url,
        publish_time: new Date(),
        update_time: new Date(),
        watch_count: 3
      }
    })
  } catch (e) {
    console.error(e)
  }
}

Getting the post list : A cloud function queries the collection, selects required fields, and orders by update_time descending:

exports.main = async (event, context) => {
  return {
    postlist: await db.collection('post_collection')
      .field({ _id: true, author_name: true, content: true, title: true, watch_count: true })
      .orderBy('update_time', 'desc')
      .get()
  }
}

Viewing post details : The client passes a post ID, the cloud function retrieves the full document, then the client downloads each image file ID:

exports.main = async (event, context) => {
  return {
    postdetail: await db.collection('post_collection')
      .where({ _id: event.postid })
      .get()
  }
}

Image downloading logic:

downloadImages: function(image_urls) {
  var that = this;
  if (image_urls.length == 0) return;
  var urls = [];
  for (let i = 0; i < image_urls.length; i++) {
    wx.cloud.downloadFile({
      fileID: image_urls[i],
      success: res => {
        console.log(res.tempFilePath);
        urls.push(res.tempFilePath);
        if (urls.length == image_urls.length) {
          console.log(urls);
          that.setData({ imageUrls: urls, imagesLoaded: true });
        }
      },
      fail: err => {
        // handle error
      }
    })
  }
}

Commenting : The logic mirrors post publishing, differing only in the data fields written.

Conclusion : Cloud development reduces backend overhead and speeds up development for information‑centric mini‑programs (e.g., news or video apps). However, it has limitations such as modest database size (2 GB) and storage (5 GB), and may not suit use‑cases requiring heavy data aggregation (e.g., RSS readers). For simple user‑generated content apps, cloud development offers clear efficiency benefits.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaScriptdatabaseMini ProgramCloud FunctionsWeChatcloud development
Tencent Cloud Developer
Written by

Tencent Cloud Developer

Official Tencent Cloud community account that brings together developers, shares practical tech insights, and fosters an influential tech exchange community.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.